7

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?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
fweigl
  • 21,278
  • 20
  • 114
  • 205

1 Answers1

14

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.

Yuliia Ashomok
  • 8,336
  • 2
  • 60
  • 69
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88