1

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?

ashwin mahajan
  • 1,654
  • 5
  • 27
  • 50
  • 1
    Can you add `Timeber.i("isDisposed=${compositeDisposables.isDisposed()}")` just before the add line to confirm it is not disposed – Sanlok Lee Jul 10 '19 at 22:45
  • @SanlokLee It is true. – ashwin mahajan Jul 11 '19 at 06:39
  • 3
    Check if the `compositeDisposable` was disposed before, because if it was, it will instantly dispose any disposable that is newly added to it – Ahmed Ashraf Jul 11 '19 at 11:59
  • @AhmedAshrafGamal Yes, this disposable is kept with the intention of disposing multiple times. So every time it is disposed, does it need a new composite disposable instance? – ashwin mahajan Jul 11 '19 at 13:10
  • Thanks @AhmedAshrafGamal. Assigning a new instance to the variable every time it is disposed works. Please let me know if this is not the right way of handling this. – ashwin mahajan Jul 11 '19 at 13:37
  • 1
    If you want to dispose multiple times but want to keep the same `CompositeDisposable` instance, use `CompositeDisposable.clear()` instead of `CompositeDisposable.dispose()` – Sanlok Lee Jul 11 '19 at 14:39
  • @SanlokLee Subscriptions get disposed with the clear method as well. – ashwin mahajan Jul 11 '19 at 16:43
  • @AshwinMahajan, using `clear()` should let disposables to be added again. Can you show us the part where you call `clear()` or `dispose()`? – Sanlok Lee Jul 11 '19 at 20:43
  • @ashwinmahajan no you don't need a new composite disposable everytime, like Sanlok Lee said, if it's intended to be disposed multiple times, use clear() instead of dispose(), refer to this answer to know the difference https://stackoverflow.com/questions/47057885/when-to-call-dispose-and-clear-on-compositedisposable If it's not intended to be cleared multiple times, please show us the code so that we can help – Ahmed Ashraf Jul 11 '19 at 22:21

1 Answers1

2

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:

  1. Instantiate a new CompositeDisposable() and start adding dispose objects. Once you are done you call dispose(). If you want to add something more, instantiate CompositeDispose again.
  2. Instantiate a 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
Bugdr0id
  • 2,962
  • 6
  • 35
  • 59