我是否必须取消订阅已完成的 observable? [英] Do I have to unsubscribe from completed observable?
问题描述
如果一个 observable 完成,我是否仍然需要取消订阅/处置(在 RxJava2 中)该 observable 以移除 Observer(防止内存泄漏),或者在 onComplete
或 onComplete
或 onError
事件发生?
If an observable completes, do I still have to unsubscribe / dispose (in RxJava2) the observable to remove the Observer (prevent memory leaks) or is this handled internally by RxJava once a onComplete
or onError
event occurs?
Single
、Completable
、Flowable
等其他类型怎么样
what about other types like Single
, Completable
, Flowable
etc.
推荐答案
是的,你是对的.
流终止后( onComplete/onError 已被调用),订阅者自动取消订阅.您应该能够使用 Subscription 对象上的 isUnsubscribed()
方法测试这些行为.
After a stream is terminated ( onComplete / onError has been called ), subscriber unsubscribes automatically. You should be able to test these behaviors using isUnsubscribed()
method on the Subscription object.
这篇关于我是否必须取消订阅已完成的 observable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!