RX Java – what is compositedispose in rxjava

I'm an Android student I think leans RX Java My question is "what is compositedisposal in Rx Java" PL detailed description

Solution

Compound one-time processing (it is easier to cancel in advance) Suppose you have an activity where multiple API calls occur simultaneously:

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

If you need to dispose in advance (for example, user navigation away from activities), you need to do this:

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

If you use composite disposal instead, you can store all disposable items in it like this:

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

You can then make a processing call:

composite.dispose()

If you are using kotlin, you can use operator overloading to make it look better:

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

This enables you to express:

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

Disposable means that the request (considered to be completing work) and there is a method called dispose to process the request

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>