2 years ago

#55623

test-img

Some Noob Student

RxJava: How to subscribe to 2 observables in order and merge their result?

To set the stage, I have 2 network calls, the 2nd one depends on the result of the 1st.

fun netCall1(): Observable<Data1>{...}

fun netCall2(data: Data1): Observable<Data2>{...}

How do I create a stream that subscribes to netCall1(), uses the result to subscribe to netCall2(), then merges the results of both calls and return that?

net1: --1--2----------3-------- 
net2: -----------a-b--c--------
res : -----------P(1,a)--P(2,b)-P(3,c)---------
(network calls may retry up to 3 times, P means Pair<Data1,Data2>)

This is my attempt, but I'm unsure how to merge the results together.

fun myAttempt(): Observable<Pair<Data1, Data2>>{
  Observable.just()
    .flatMap{ netCall1() }
    .flatMap{ data1 -> netCall2(data1) }
    // unsure how to proceed...
}

Is this possible?

kotlin

rx-java

rx-java3

0 Answers

Your Answer

Accepted video resources