Search code examples
androidrx-java2

Do I need to check isDisposed() before calling dispose()?


I'm learning RxJava and I noticed a lot of the sample code does a isDisposed() check before calling dispose(). I did not notice any issues when I called dispose() on an already disposed Disposable.

So my question is, do I need the isDisposed() check? Are there situations where I should check isDisposed() before disposing? What are the pros and cons on doing the check first?


Solution

  • It makes little sense to call isDisposed. dispose implementations already do that for you and make sure repeated calls are no-ops or have no detectable effect.

    Unfortunately, somebody in the early days of RxJava started writing examples with it and now everybody keeps copying that pattern.

    It makes a little more sense to check isDisposed before calling onNext for example but you don't get to do that very often outside Observable.create().