RxKotlin icon indicating copy to clipboard operation
RxKotlin copied to clipboard

Do we need to call onComplete() of subject in onDestroy() method of activity ?

Open bembem1011 opened this issue 5 years ago • 2 comments

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()
}

bembem1011 avatar Sep 06 '19 03:09 bembem1011

For what? If you haven't supposed to do anything on onComplete on the observer you don't need to do that.

AgiMaulana avatar Dec 02 '19 12:12 AgiMaulana

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:

  1. https://blog.mindorks.com/understanding-rxjava-subject-publish-replay-behavior-and-async-subject-224d663d452f
  2. http://reactivex.io/RxJava/javadoc/rx/subjects/PublishSubject.html

WendyYanto avatar May 11 '20 16:05 WendyYanto