What is CompositeDisposable in RxJava

AndroidRx Java

Android Problem Overview


I am an Android Student. I want to learn RxJava. My Question is "What is CompositeDisposable in RxJava?". Please describe in detail.

Android Solutions


Solution 1 - Android

Composite disposable makes disposing (think cancelling early easier). Say you have an activity that has multiple api calls happening at once:

var disposable = api.call1(arg1, arg2).subscribe(...)
var disposable2 = api.call2(arg1).subscribe(...)
var disposable3 = api.call3().subscribe()

If you need to prematurely dispose (e.g. the user navigating away from the activity) then you'd need to do this:

disposable.dispose()
disposable2.dispose()
disposable3.dispose()

If you instead use a CompositeDisposable you can store all of the disposables in it. Like so:

val composite = CompositeDisposable()
composite.add(api.call1(arg1, arg2).subscribe(...))
composite.add(api.call2(arg1).subscribe(...))
composite.add(api.call3().subscribe())

And then you can make one dispose call instead:

composite.dispose()

If you are using kotlin you can use operator overloading to make this look nicer:

  operator fun CompositeDisposable.plusAssign(disposable: Disposable){
        this.add(disposable)
    }

Which enables you to express it as:

val composite = CompositeDisposable()
composite += api.call1(arg1, arg2).subscribe(...)
composite += api.call2(arg1).subscribe(...)
composite += api.call3().subscribe()

Disposable signifies a request (think work being done) and has a method called dispose for disposing of the request.

Solution 2 - Android

CompositeDisposable is just a class to keep all your disposables in the same place to you can dispose all of then at once. Like:

Disposable disposable1;
Disposable disposable2;
Disposable disposable3;

CompositeDisposable composite = new CompositeDisposable();
composite.addAll(disposable1, disposable2, disposable3)
composite.dispose()

All of then are disposed

Solution 3 - Android

CompositeDisposable is a convenient class for bundling up multiple Disposables, so that you can dispose them all with one method call on CompositeDisposable.

You can add disposables to a CompositeDisposable with CompositeDisposable#add

Instead of calling dispose() on each Disposable individually, you call CompositeDisposable#clear() to dispose all Disposables that have been added. If you want to dispose all current Disposables and automatically dispose any Disposables that are added in the future, call CompositeDisposable#dispose(). It kind of makes sense, you are literally disposing the CompositeDisposable when you call dispose() on it, so it makes sense that any Disposables you add are disposed.

Solution 4 - Android

Adding to the above CompositeDisposable offers consistent time space complexity of O(n) irrespective of the addition and removal of multiple disposables, which is also cleared when activity or fragment is destroyed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTesterView Question on Stackoverflow
Solution 1 - AndroidJim BacaView Answer on Stackoverflow
Solution 2 - AndroidLeandro Borges FerreiraView Answer on Stackoverflow
Solution 3 - AndroidAndrew GallaschView Answer on Stackoverflow
Solution 4 - AndroidAjay DeepakView Answer on Stackoverflow