Reputation: 808
I have 3 Disposaple objects in my CompositeDisposable:
CompositeDisposable disposables = new CompositeDisposable();
disposables.add(
interactor1
.someMethod1()
.subscribe(...some handling logic));
disposables.add(
interactor
.someMethod2()
.subscribe(...some handling logic));
disposables.add(
interactor
.someMethod3()
.subscribe(...some handling logic));
The first two methods return some data and transmit it to other methods. Third method must be called when first two completed. How can I do it?
Example methods signature:
Single<List<String>> someMethod1();
Single<List<Integer>> someMethod2();
Single<String> someMethod3();
Upvotes: 0
Views: 76
Reputation: 4005
An example is worth a thousand words:
public void SO() {
Single.zip(someMethod1(),
someMethod2(),
Pair::create) // (1) create pair from results of someMethod1 and someMethod2
.flatMap(methodsResultPair -> {
// (2) handling logic of combined result
// methodsResultPair.first is result of someMethod1
// methodsResultPair.second is result of someMethod2
return someMethod3();
})
.subscribe((resultOfMethod3, throwable) -> {
// process resultOfMethod3
// process error
});
}
public Single<List<String>> someMethod1() {
return Single.just(Collections.<String>emptyList())
.doOnSuccess(objects -> { /* (3) handling logic of someMethod1 result */ });
}
public Single<List<Integer>> someMethod2() {
return Single.just(Collections.<Integer>emptyList())
.doOnSuccess(objects -> { /* (4) handling logic of someMethod2 result */ });
}
public Single<String> someMethod3() {
return Single.just("");
}
With this you have one disposable instead of three, because all of them are now part of one reactive chain. Single.just()
methods are used just for providing some dummy data.
Upvotes: 2
Reputation: 2714
You would zip the first 2 observables and then use flatmap on the output of that. The zip function would project an item when both observables have produced a value, the combined object would then be passed to the flatmap which could then be passed to your third observable function.
Upvotes: 0