Here is my question: I have some singles and want to zip them. But I only want the zip function to be called after a Completable has completed. Also I want to subscribe to all singles and the Completable at the same time. (So no completable.andThen(Single.zip(...)
)
Here is an example of what I'm doing now:
Single<T1> s1 = …;
Single<T2> s2 = …;
Single<T3> s3 = …;
Completable c = …;
Single.zip(s1, s2, s3, c.andThen(Single.just("")), (a, b, c, ignore) -> {
// All singles have emitted an item and c is completed
…
})
Is there a better way?
Single is an Observable which only emits one item or throws an error. Single emits only one value and applying some of the operator makes no sense.
That can be completed.
from the documentation, Single.fromCallable() : /** * Returns a {@link Single} that invokes passed function and emits its result for each new SingleObserver that subscribes. * <p> * Allows you to defer execution of passed function until SingleObserver subscribes to the {@link Single}. * It makes passed function "lazy".
The Zip method returns an Observable that applies a function of your choosing to the combination of items emitted, in sequence, by two (or more) other Observables, with the results of this function becoming the items emitted by the returned Observable.
You could use toSingleDefault
when converting from Completable
to Single
:
Single<T1> s1 = …;
Single<T2> s2 = …;
Single<T3> s3 = …;
Completable c = …;
Single.zip(s1, s2, s3, c.toSingleDefault(""), (a, b, c, ignore) -> {
// All singles have emitted an item and c is completed
…
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With