How to repeat the same network request (different parameters) multiple times in RxJava/RxAndroid?… here is a solution to the problem.
How to repeat the same network request (different parameters) multiple times in RxJava/RxAndroid?
So, I have a
/download
API that returns a generic Object
(based on an index number, which is its own parameter) and then I have to save it to my database, and if the transaction succeeds, I have to increment the index and repeat the same process again, otherwise retry().
I need to repeat about 50 times.
How do I implement this process using Rx-Java?
I’m stuck now. Any help is excellent. Thank you.
Solution
Observable.range(1, 50)
.flatMap(index -> // for every index make new request
makeRequest(index) // this shall return Observable<Response>
.retry(N) // on error => retry this request N times
)
.subscribe(response -> saveToDb(response));
Responses to comments (new requests are made only after the previous response has been saved to the database):
Observable.range(1, 50)
.flatMap(index -> // for every index make new request
makeRequest(index) // this shall return Observable<Response>
.retry(N) // on error => retry this request N times
.map(response -> saveToDb(response)), // save and report success
1 // limit concurrency to single request-save
)
.subscribe();