2

I'm a newbie in RxJava and I was doing some RESTful programming using RxJava2. I had few API calls to be made which were all independent with respect to each other. What I observed from my use case is that, since the API calls were being async wrt each other and were all returning Observables<String>, and from all the API responses I was doing some computations, So at the time of computation, I didn't had responses from few APIs yet and as such it failed. For all those APIs whose response was not yet received, I was making use of subscribe like the below code :

Observable<String> res = someApiCall(data1,data2);
res.subscribe(data -> { //Call Another Method.})

And for the ones for which response was received the code was :

return someApiCall.flatMap(data -> { // Call Another Method})

My Question is : Does using FlatMap makes it Blocking ? How does the 2 flows that i described above differ ? Is Subscribe always in async ?

DebashisDeb
  • 392
  • 5
  • 13
  • This may be helpful. https://stackoverflow.com/a/33474970/9080015 – curlyBraces Feb 13 '18 at 07:10
  • The current implementation of `flatMap()` is ***eager*** in contrast to most of the `Stream` methods. It populates the stream completely before returning, so the first `Call Another Method` that blocks (or if `someApiCall` blocks) will block the stream. – Jim Garrison Feb 13 '18 at 07:14

1 Answers1

2

Subscriber in Rx it´s sync by default. The only way to make it run async your pipeline is using subscribeOn or observerOn operators.

https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/scheduler/ObservableAsynchronous.java

Also the use of flatMap in your case it´s correct, you must use the flatMap in order to compose functions, that call new API with the previous API response information.

And in case some of those API calls are not responding, you can always use timeout in your flatMap operator to do a compensation.

http://reactivex.io/documentation/operators/timeout.html

paul
  • 12,873
  • 23
  • 91
  • 153