android - 使用 rxbinding 时我应该取消订阅吗?

标签 android rx-java kotlin rx-binding

这是我如何在 Kotlin 中使用 RxBinding:

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    reset_password_text_view.clicks().subscribe { presenter.showConfirmSecretQuestionBeforeResetPassword() }
    password_edit_text.textChanges().skip(1).subscribe { presenter.onPasswordChanged(it.toString()) }
    password_edit_text.editorActionEvents().subscribe { presenter.done(password_edit_text.text.toString()) }
}

Observable.subscribe(action) 返回 Subscription。我应该保留它作为引用并取消订阅 onPause() 还是 onDestroy()

像这样:

private lateinit var resetPasswordClicksSubs: Subscription

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    resetPasswordClicksSubs = reset_password_text_view.clicks().subscribe { presenter.showConfirmSecretQuestionBeforeResetPassword() }
}

override fun onDestroy() {
    super.onDestroy()
    resetPasswordClicksSubs.unsubscribe()
}

最佳答案

我认为 Jake Wharton(图书馆的创建者)给了 best answer :

Treat a subscribed RxView.clicks() (or any Observable from this library for that matter) like you would the View reference itself. If you pass it (or subscribe to it) somewhere outside the lifetime of the View, you've just leaked your entire activity.

So if you're just subscribing inside your ViewHolder there's no need to unsubscribe just like there'd be no need to unregister a click listener were you doing it manually.

关于android - 使用 rxbinding 时我应该取消订阅吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41482036/

相关文章:

java - 我如何知道我在 Resume 上启动的任务正在运行?

android - 在 Android 中将文本作为 SeekBar 的背景

android - ACTION_IMAGE_CAPTURE Intent : Avoid Activity being destroyed/process being killed

java - RxJava : Dynamic set of observables

android - 从 rx java 表达式转换为 lambda

java - junit 3 的故障跟踪窗口不显示预期值

java - 在 rxjava 中使用一系列 Observable 进行一对多映射

docker - Docker 容器中的 Spring Boot 资源

android-studio - 如何使用Kotlin安全地强制转换readLine()的结果以防止类型不匹配

kotlin - 使用Kotlin和内容辅助的动态dsl