The Uber Solution: AutoDispose

The downside of LiveDataReactiveStreams, to some, is that you wind up with a LiveData object. Some developers will prefer to stick with RxJava throughout, but still would like some measure of automatic lifecycle-based subscription cleanup.

For that, Uber offers AutoDispose.

It allows you to add lifecycle-based subscription cleanup with a single line added to your RxJava chain, akin to:

observable
  // subscribeOn(), observeOn(), map(), and so on go here
  .to(AutoDispose.with(scope).forObservable())
  .subscribe(/* good stuff here */);

Here, scope will be an object that provides lifecycle details to AutoDispose, so that it knows when to stop forwarding events on to your Consumer or other subscriber. There are two Android-specific classes for this:

If you are comfortable with consuming events in your UI using LiveData, LiveDataReactiveStreams is likely to be the simpler choice. If, however, you are interested in avoiding the conversion to LiveData, AutoDispose is worth considering.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.