Edu Barbas
Edu Barbas

Reputation: 467

RxJava - repeat API call until result is complete

I have an API that returns FamilyDetails. I use retrofit to fetch them, and it is modelled as follows:

Observable<FamilyDetails> getFamilyDetails(@Query int personId);

Once fetched, those details will be used to update a Person. My API delivers those FamilyDetails as soon as it has some information to show, which means that the details may be incomplete, and thus there is a complete = false flag delivered in responses when the server hasn't finished fetching all family details - in turn, my app should send another request to fetch the remaining FamilyDetails if we receive complete = false in the first response, and keep doing so for as long as the details remain incomplete.

I have "somewhat" achieved what I wanted, but not entirely with this code:

personApiService.getFamilyDetails(personId)
    // repeat call
    .flatMap(familyDetails -> personApiService.getFamilyDetails(personId))
    // until family details are complete
    .takeUntil(familyDetails -> familyDetails.isComplete())
    // update the person object with the family details
    .flatMap(familyDetails -> Observable.just(updatePerson(familyDetails))
    //subscribe!
    .subscribe(personSubscriber);

My personSubscriber returns an updated Person.

The problem I have with this implementation is that partial updates don't go through personSubscriber's onNext(), as I only get one call to onNext() with the updated Person object with its complete FamilyDetails.

I would like to model this using RxJava with the following requirements:

Thanks in advance!

Upvotes: 5

Views: 5320

Answers (1)

JohnWowUs
JohnWowUs

Reputation: 3083

personApiService.getFamilyDetails(personId)
// repeat call
.repeat()
// until family details are complete
.takeUntil(familyDetails -> familyDetails.isComplete())
//subscribe (do your updates here)
.subscribe(personSubscriber);

Upvotes: 13

Related Questions