Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single.zip with Completable

Tags:

rx-java

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?

like image 767
PGuertler Avatar asked Sep 27 '16 12:09

PGuertler


People also ask

What is single <> in Java?

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.

What is a Completable?

That can be completed.

What is single fromCallable?

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".

What is Observable zip?

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.


1 Answers

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
…
})
like image 142
akarnokd Avatar answered Jan 04 '23 03:01

akarnokd