0
votes

In RXJava, I have a 2 observables which are responses from 2 downstream calls.One downstream call is a long poll call, other is a short one and returns right away. I am using the Observable.zip to combine the responses of both the responses.The below code works fine.

Observable
 .zip(observable1, observable2)
 .flatMap(update -> foo(update));

Now what I want to implement is that if the output of the short downstream call (observable1) does not content a specific value, then skip the zip i.e dont wait for the output of the longer downstream call (observable2). I tried to implement it in the below way, but if the condition is true it doesn't zip with the observable2, but it does not even emit observable1 response.

Observable finalresponse = observable1
                .takeWhile(obsResponse1 ->  checkIfValueExist(obsResponse1))
                .zipWith(observable2,  (observable1, observable2) -> execute(observable1, observable2))
                .flatMap(update -> main.execute(update));
1
why u making parallel call? Why not first make a call to downstream call which is the shortest, check the value and based on that make the long call - Niraj Chauhan
These are just two of the several calls that area made. For optimization I cant have these serial. - BigDataLearner
In your case, sequential calls will be more performant as u will not occupy memory for the second call at the very first place. - Niraj Chauhan

1 Answers

0
votes

In zip there is a rule it will return only if both of streams will emit an item so what you need to do is to filter or return Observable.empty() in observable if your object is not what you expect or you can use filter

   Observable
            .zip(Observable.just(1).filter(integer -> integer==1), Observable.just(2).filter(integer -> integer==3),(integer, integer2) -> integer)
            .flatMap(update -> foo(update));