I am using Room with RxJava/RxKotlin Flowable following this article. I got it running but the got issue using it with ViewPager with 3 fragments.
I will walk you through my code little bit:
I have a viewpager with tab layout and three fragments(A, B, and favorites). First two fragments contain lists of data which can be added to favorite.
In favorite fragment, I am using Flowable to listen to changes made by A and B and update list accordingly. But what happens is when an item is made favorite in A and B, app crashes because the Flowable subscription in favorite fragment runs even when the fragment is not in foreground.
What I want is to be able to stop the subscription when the fragment is not in foreground and start in foreground.
I tried to stop it in onPause method of favorite fragment but flowable has no unsubscribe or dispose method.
My code is
dbRepository?.getAllImportant()?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(getFlowableSubscriber())
Thank you for answers. I found a work around. By saving Subscription in onSubscribe(subscription: Subscription)
function of FlowableSubscriber. And later calling subscription.cancel
in onStop()
override fun onSubscribe(subscription: Subscription) {
log("onSubscribe")
this@ImportantFragment.subscription = subscription
}
And
override fun onStop() {
log("onStop() ")
this.subscription.cancel()
super.onStop()
}