profile
viewpoint

Ask questionscombineLatest-Function not called when first parameter emits multiple items (1.3)

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.

ReactiveX/RxJava

Answer questions angelsanzn

It would be nice if this got documented in the Javadoc for the method. It is a really subtle behavior that can lead to nasty bugs.

useful!

Related questions

No questions were found.
source:https://uonfu.com/
Github User Rank List