Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to call dispose and clear on CompositeDisposable

People also ask

When should I dispose of disposable RxJava?

It goes to the backstack, therefore you don't need to create a new instance of CompositeDisposable every time when a fragment's view is re-created. If you have CompositeDisposable in activities, then call dispose() upon onDestroy() .

What is dispose in RxJava?

Tuesday. January 29, 2019 - 5 mins. The Disposable is a link between an Observable and an active Observer , calling its dispose() method stops the emissions and dispose of all resources used for that Observer : fun main() { val seconds = Observable.

What is the use of disposable in Android?

A Disposable is a stream or a link between an Observable and an Observer . A quick check of the documentation shows that it has two main methods, dispose() and isDisposed() . The former disposes of the link, while the latter checks if the link has been disposed.

What is composite disposable in RxJava?

A disposable container that can hold onto multiple other disposables and offers O(1) add and removal complexity.


You're right, you can save yourself from creating a new CompositeDisposable for each time the corresponding view is created, but instead treat a CompositeDisposable as a single instance tied to the onCreate/onDestroy lifecycle methods and treat aggregated disposables as part of the fragment view calling clear in onDestroyView.


Instead of using CompositeDisposable.dispose(), you can better use CompositeDisposable.clear() so you don't have to create a new CompositeDisposable: method documentation.

clear() //Atomically clears the container, then disposes all the previously contained Disposables.

dispose() //Dispose the resource, the operation should be idempotent.

private final CompositeDisposable disposables = new CompositeDisposable();


// adding an Observable to the disposable
disposables.add(sampleObservable()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<String>() {
                    @Override
                    public void onComplete() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(String value) {
                    }
                }));

    static Observable<String> sampleObservable() {
        return Observable.defer(new Callable<ObservableSource<? extends String>>() {
            @Override
            public ObservableSource<? extends String> call() throws Exception {
                // Do some long running operation
                SystemClock.sleep(2000);
                return Observable.just("one", "two", "three", "four", "five");
            }
        });
    }                


// Using clear will clear all, but can accept new disposable
disposables.clear(); 
// Using dispose will clear all and set isDisposed = true, so it will not accept any new disposable
disposables.dispose(); 
  • Clear - onStop()
  • Dispose - onDestroy()

I have created a sample project to demonstrate the use of RxJava2. Here you can find the sample project - RxJava2-Android-Samples