Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two Observable that emit different types [duplicate]

Tags:

java

rx-java

I have two Observables, one Observable<String>, one Observable<Boolean>. Can I combine them so I receive

@Override
public void call(String s, Boolean b) {

}

when both operations are finished?

like image 612
fweigl Avatar asked Nov 07 '15 12:11

fweigl


1 Answers

When you want to wait for items to be emitted from two observables (synchronizing them) you usually want something like Observable.zip:

Observable<String> o1 = Observable.just("a", "b", "c");
Observable<Integer> o2 = Observable.just(1, 2, 3);
Observable<String> result = Observable.zip(o1, o2, (a, b) -> a + b);

result will be an observable generating the application of (a, b) -> a + b to o1's and o2's items. Resulting in an observable yielding "a1", "b2", "c3".

You can also use Obervable.zipWith with an actual instance to get the same effect.

Note that this will terminate on the shorter observable when there is nothing to zip with.

like image 167
Reut Sharabani Avatar answered Sep 22 '22 15:09

Reut Sharabani