Search code examples
rx-java2

Should dispose a rx-java Maybe?


If I have this rxjava chain:

Observable.create { ... }
  .firstElement()
  .subscribe( {...}, {...})

After experimenting and looking at the sources, it looks like firstElement() automatically dispose the up stream (which makes sense), so I don't have to care about it. Is that right? Ok then. My question is, should I dispose the Maybe returned by firstElement()? I put a .doOnDispose() callback after firstElement() and checked that it doesn't happen automatically. Does that mean that I shouldn't care? If a Maybe can emit no more than one item


Solution

  • Short answer: You should don't care.

    Longer answer: doOnDispose() is only called when Observable is disposed explicitly (by disposable.dispose()) but it will not be invoked when Observable calls onComplete(). If you want to check it by yourself instead of doOnDispose() you should us doFinally().

    Peace!