I am an Android Student. I want to learn RxJava. My Question is "What is CompositeDisposable in RxJava?". Please describe in detail.
4 Answers
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.

- 58,567
- 58
- 222
- 373

- 6,112
- 2
- 24
- 30
-
What is Disposable ? – Tester Oct 10 '17 at 13:02
-
What's the difference between this and storing disposables in a list? – Mateen Ulhaq Oct 15 '19 at 07:57
-
Not much, other than you can call dispose on the CompositeDisposable without having to iterate through all the items in your list. – Jim Baca Dec 27 '19 at 16:18
-
This is great! So I can dispose all subscription in just one call like `composite.dispose()` after I added each `Observable` or `Single`? – Bitwise DEVS Apr 25 '21 at 14:10
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

- 12,422
- 10
- 53
- 73
-
4
-
3It is an interface for a class that can be disposed. Observable, Single, Completable and Maybe all implements this interface. It is important to dispose your observables to avoid memory leaks – Leandro Borges Ferreira Oct 10 '17 at 13:41
-
What is difference between CompositeDisposable vs SerialDisposable – praveen2034 Apr 17 '19 at 11:10
-
@praveen2034 `compositeDisposable` contains list of disposables, while `SerialDisposable` is a one disposable, that can be replaced or updated with new disposable. More details about the difference are here: https://rb.gy/9vir4y – AI I Jun 21 '21 at 04:32
CompositeDisposable
is a convenient class for bundling up multiple Disposable
s, 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 Disposable
s that have been added. If you want to dispose all current Disposable
s and automatically dispose any Disposable
s 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 Disposable
s you add are disposed.

- 2,596
- 2
- 19
- 26
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.

- 471
- 5
- 9