RxJava2 onSubscribe and onComplete

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



RxJava2 onSubscribe and onComplete



I'm learning RxJava2, and I have a question about proper usage to prevent memory leaks.



Do I need to ensure that onComplete() gets called for every time onSubscribe() gets called?



I have a stream that's set up like this:


Observables.combineLatest(
siteDataController
.GetCurrentSelectionObservable()
.distinctUntilChanged site -> site.GetStid() ,
uiController
.GetDisplayFieldsObservable()
.observeOn(AndroidSchedulers.mainThread())
) site, data -> Pair(site, data)
.takeWhilecontinueUpdates.get()
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Observer<Pair<SiteDataController.ProcessedSite, UIController.DisplayFields>>
override fun onComplete()
Log.e("Lifespan", "Site / UI onComplete()")

override fun onSubscribe(d: Disposable)
Log.e("Lifespan", "Site / UI onSubscribe()")
disposable = d


override fun onNext(t: Pair<SiteDataController.ProcessedSite, UIController.DisplayFields>)
// use result to build out ui


override fun onError(e: Throwable)
e.printStackTrace()

)



This gets set up in the fragment's onResume(). Also, "continueUpdates" gets set to true in onResume() and false in onPause(). The roots of both Observables that make up the combineLatest() stream are BehaviorSubjects. But despite the takeWhile condition being switched to false, and even despite calling onComplete() on both BehaviorSubjects, the onComplete() on the observer never gets called. Am I doing something wrong?





You seem to be recreating this flow over and over. You should be disposing the disposable in onPause to avoid any memory leak. Also the predicate of takeWhile gets evaluated only when an item passes through so setting that reference to false outside of such invocation has no "completion" effect.
– akarnokd
Aug 12 at 18:39


disposable


takeWhile





Yes, forgot to mention, I am calling dispose on the disposable, I only removed it so I could ensure onComplete being called. But will simply calling dispose() suffice? Does the observable even have any knowledge of what observers it has, and will it know to clear itself up when it has no more observers?
– KairisCharm
Aug 12 at 18:41






Yes, the chain cleans up after itself upon termination or disposing it. However, you should also "forget" any references to it, including the disposable.
– akarnokd
Aug 12 at 18:45





Ok, by "forget," you mean: disposable?.dispose() disposable = null
– KairisCharm
Aug 12 at 18:46



disposable?.dispose()


disposable = null





Yes, if you track them individually, null the references out (and ?. is advised) or if you collect them into a ComposideDisposable, clear the composite.
– akarnokd
Aug 12 at 18:48


ComposideDisposable









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard