0

I just started on ReactiveX and Retrofit, Consider following example retrofit example,

@GET
public Observable<ResponseType1> makeFirstCall();

@POST
public Observable<ResponseType2 makeSecondCallWithFirstResponse(@Body ResponseType1 input);

Is it a good idea to have observable within another action1? like below

makeFirstCalle().subscribe((responseType1) -> {

    makeSecondCallWithFirstResponse(responseType1).subscribe("second action goes here")

});
Hareesh
  • 694
  • 6
  • 18

1 Answers1

2

Why not use concatMap or flatMap?

makeFirstCall().concatMap(responseType1 -> makeSecondCallWithFirstResponse(responseType1))
               .subscribe(....)

You can keep chaining if you have additional api calls. For example

makeFirstCall().concatMap(responseType1 -> makeSecondCallWithFirstResponse(responseType1))
               .concatMap(responseType2 -> makeThirdCallWithSecondResponse(responseType2))
               .subscribe(....)
JohnWowUs
  • 3,053
  • 1
  • 13
  • 20
  • I have to more than 3 api calls, which I have to call based on the previous response. Is this the only one way? Or there are any options are there? – Hareesh May 16 '16 at 13:42
  • If you want to chain observables in the way you described, I can't see any other way of doing it. What's the problem with flatMap or concatMap? – JohnWowUs May 16 '16 at 14:15
  • I don't know I just asked. – Hareesh May 17 '16 at 07:04