# RxJava: Disposables Concept `Android` `RxJava` `Disposables` [TOC] ## What are Disposables and why we need them? First, see the image below: ![](https://i.imgur.com/K5IUYz7.png =500x330) Now let's assume that the LiveData(observable) is actually in an activity and it has 3 observers subscribed. However, the activity containing the observable is destroyed. That would mean that observers are no longer needed as the object being observed is no longer available. Then, we do **NOT** need observers anymore! That is why Disposables are needed! If the observers are marked as disposables and can be cleared when observables are gone, it would help the system to clear up some space too. ## How to use disposables? ### 1. Set Up Observables Firstly, we need to have our observables in order to subscribe observers, that we will later dispose them. ``` java=== Observable<ClassType> observableObject = Observable .fromIterable(DataSource.createObservableList()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); ``` >>**NOTE:**[color=red] >1. **Observable** Create a new Observable object. >2. **.fromIterable()** turns list of object in this case into an observable > .fromIterable Operator is one of the main Operators in RxJava >3. **.subscribeOn()** > Specify worker thread, which means telling those subscribers where the thing they want to subscribe is working at. (ex:Background) >4. **.observeOn()** > designate observer thread (ex: main thread) > >**PS:** >* Observable<ClassType> is from io.reativex and is an observable of type ClassType. >* .fromIterable Operator is one of the main Operators in RxJava [color=pink] ### Subscribe Observers to observables Once the observables are made, then we need the observers. All observers are disposable by extending [the DisposableObserver abstract class](http://reactivex.io/RxJava/javadoc/io/reactivex/observers/DisposableObserver.html). ```java== observableObject.subscribe(new io.reactivex.Observer<ClassType>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(Country value) { } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); ``` ### 3. Set Up a Disposable Container >**Why we need a container?** >Disposable container are used to hold disposable observers. And as soon as we do not need them, we can clear them away. [color=pink] **How to set up a Disposable Container?** ```java== CompositeDisposable disposContainer = new CompositeDisposable(); ``` ### 4. Add Disposable to the container We can add it in the override methods: onSubscribe, which gives us the Disposable. Or we can add it directly to disposContainer if we made a class extending DisposableObserver<T>. ```java=== @Override public void onSubscribe(Disposable d) { disposContainer.add(d); } ``` ### 5. Clear Disposables! When observers are not needed anymore, we can dispose them. >**When to clear them?** >* **Activity/Fragment:** onDestroy(); >* **ViewModel:** onCleared() [color=pink] **How to Clear Disposables?** ```java=== disposContainer.clear(); ```