Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is CompositeDisposable in RxJava

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

like image 232
Tester Avatar asked Oct 09 '17 21:10

Tester


People also ask

What is the use of CompositeDisposable?

CompositeDisposable is a convenient class for bundling up multiple Disposable s, so that you can dispose them all with one method call on CompositeDisposable . Instead of calling dispose() on each Disposable individually, you call CompositeDisposable#clear() to dispose all Disposable s that have been added.

What are disposables in RxJava?

RxJava 2 introduced the concept of Disposables . Disposables are streams/links/connections between Observer and Observable . They're meant to give power to the Observers to control Observables . In most cases, the Observer is a UI element, ie; Fragment , Activity , or a corresponding viewmodel of those elements.

What is CompositeDisposable in Kotlin?

CompositeDisposable is a class from RxJava 2, which you're not adding directly into your build.

What is RX Java in Android?

RxJava is a Java library that enables Functional Reactive Programming in Android development. It raises the level of abstraction around threading in order to simplify the implementation of complex concurrent behavior.


2 Answers

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

like image 39
Leandro Borges Ferreira Avatar answered Sep 20 '22 15:09

Leandro Borges Ferreira


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.

like image 96
Jim Baca Avatar answered Sep 16 '22 15:09

Jim Baca