gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:35:33 26 4
gpt4 key购买 nike

这是我如何在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com