Reputation: 1607
I need to implement synchronous calls with RxJava
and Retrofit
.I have an ArrayList
of ids. I need to iterate this array and make the call to the web server for each id using Retrofit
but I know how to do this only async, could U tell me how to do this like in queue when after one call finished the next one starts.
Upvotes: 2
Views: 2280
Reputation: 96
This code will execute them synchronious
Observable.from(ids)
.map(id -> callToWebServer(id).toBlocking().first())
But you need to handle all network errors from callToWebServer() carefully in map().
Upvotes: 1
Reputation: 39853
Your question is worded quite ambiguous, but I think I might have understood it. Starting from a list of items you can create an observable of these with from()
. This values can be mapped afterwards your API calls. Using concatMap()
guarantees the order of your results, so you effectively get an observable over your results. Which these you can do whatever you want, even call toBlocking()
on it and make the observable synchronous. But there should not be any need for this.
List<Result> results =
Observable.from(ids)
.concatMap(id -> callToWebServer(id))
.toList()
.toBlocking()
.single();
Upvotes: 1