Hi folks,
I think I discovered a bug with the combineLatest function or I just didn't understand how it's supposed to work :).
If you want to combine two observable, one that emits a single item and one that emits two items, then the function to combine the two observables only gets called twice if you pass in the observable that emits multiple items as second parameter. Here is minimal code example that probably better shows what I mean
Observable<String> case1Observable1 = Observable.just("Case 1 - Observable 1 - String 1").startWith("Case 1 - Observable 1 - String 2");
Observable<String> case1Observable2 = Observable.just("Case 1 - Observable 2 - String 1");
Observable.combineLatest(case1Observable1, case1Observable2, (s, s2) -> {
System.out.println("Combining...");
System.out.println(s);
System.out.println(s2);
return "Combined " + s + "|||" + s2;
}).subscribe(s -> System.out.println("Item emitted: " + s));
Observable<String> case2Observable1 = Observable.just("Case 2 - Observable 1 - String 1");
Observable<String> case2Observable2 = Observable.just("Case 2 - Observable 2 - String 1").startWith("Case 2 - Observable 2 - String 2");
Observable.combineLatest(case2Observable1, case2Observable2, (s, s2) -> {
System.out.println("Combining...");
System.out.println(s);
System.out.println(s2);
return "Combined " + s + "|||" + s2;
}).subscribe(s -> System.out.println("Item emitted: " + s));
This produces the following output:
Combining...
Case 1 - Observable 1 - String 1
Case 1 - Observable 2 - String 1
Item emitted: Combined Case 1 - Observable 1 - String 1|||Case 1 - Observable 2 - String 1
Combining...
Case 2 - Observable 1 - String 1
Case 2 - Observable 2 - String 2
Item emitted: Combined Case 2 - Observable 1 - String 1|||Case 2 - Observable 2 - String 2
Combining...
Case 2 - Observable 1 - String 1
Case 2 - Observable 2 - String 1
Item emitted: Combined Case 2 - Observable 1 - String 1|||Case 2 - Observable 2 - String 1
I'd expect both cases to produce the same output, but as you can see, in the first case the combine function only is called once.
This is not a bug but a property of the combine function when driven by synchronous sources. The source that gets subscribed earlier will rush through its items synchronously and the operator will only remember its latest item before subscribing to the next source, and so on:
A: ----o----o--|
B: --z--|
=====================
R: --------------M--|
A: --z--|
B: ----o----o--|
======================
R: ----------M----M--|
Looks like this question has been answered. If you have further input on the issue, don't hesitate to reopen this issue or post a new one.
Most helpful comment
This is not a bug but a property of the combine function when driven by synchronous sources. The source that gets subscribed earlier will rush through its items synchronously and the operator will only remember its latest item before subscribing to the next source, and so on: