RxKotlin
RxKotlin copied to clipboard
Do we need to call onComplete() of subject in onDestroy() method of activity ?
If in ActivityA, it keep reference to
private val subjectA: PublishSubject<ObjectB> = PublishSubject.create()
private var dispose: Disposable? = null
Does we need to call onComplete() method of subject in onDestroy() method of Activity
override fun onDestroy() {
subjectA.onComplete()
dispose?.dispose()
super.onDestroy()
}
For what? If you haven't supposed to do anything on onComplete
on the observer you don't need to do that.
onComplete
is used to indicate that current streams has finished in emitting the data. When your code has finished emitting all the data needed for the observer, then you should call onComplete
for this use case to notify the observer. For me, dispose?.dispose()
is enough as it already unsubscribe the subscription to prevent any callback on destroyed activity.
Here's references to help you:
- https://blog.mindorks.com/understanding-rxjava-subject-publish-replay-behavior-and-async-subject-224d663d452f
- http://reactivex.io/RxJava/javadoc/rx/subjects/PublishSubject.html