tareq3
3/1/2019 - 1:51 PM

Disposable

Disposable:{For all Rxjava, Rxjs, all }

Disposable is used to dispose the subscription when an Observer no longer wants to listen to Observable. In android disposable are very useful in avoiding memory leaks.

Let’s say you are making a long running network call and updating the UI.

By the time network call completes its work ,

if the activity / fragment get destroyed, we will find the Observer subscription is still alive, and it tries to update already destroyed activity.

In this case it can throw a memory leak. So using the Disposables, the un-subscription can be when the activity is destroyed.

CompositeDisposable:

Consider a case where you have multiple Observables and Observers. Disposing them in Destroy one bye one is a tedious task and it can be error prone as you might forgot to dispose. In this case we can use CompositeDisposable.

CompositeDisposable:

Can maintain list of subscriptions in a pool and can dispose them all at once . Usually we call compositeDisposable.clear() in onDestroy() method, but you can call anywhere in the code.