Reputation: 1782
I'm using RxJava Flowable with Room
where I listen to changes in a table. The problem as mentioned is that when the result of subscribe, a disposable, is added to a CompositeDisposable, it immediately unsubscribes
.
Below is the relevant code :
repo.getFlowable(id)
?.doOnDispose { Timber.i("Unsubscribed") }
?.doOnDispose { Timber.i("Disposed") }
?.subscribe({
Timber.i("Subscribed")
}, {
Timber.e(it)
})?.apply {
compositeDisposables.add(this)
}
When the apply block is added, "Unsubscribed" and "disposed" are printed immediately even though "compositeDisposables.dispose()" is not called.
Without the apply block, it is working as expected.
Is this an expected behaviour? Why does the subscription gets disposed immediately when the Composite Disposable is not disposed yet?
Upvotes: 1
Views: 985
Reputation: 3063
If disposed()
is called, you won't be able to perform add()
anymore, as it will discard immediately.
There are two option solve this issue:
new CompositeDisposable()
and start adding dispose objects. Once you are done you call dispose()
. If you want to add something more, instantiate CompositeDispose
again.new CompositeDisposable()
only once, but instead of dispose()
you use clear()
. After this command you will be able to keep adding disposables, without the need to a new CompositeDispose
Upvotes: 3