Repeat retrofit call with RxJava till condition valid

Viewed 680

Problem: Hit an endpoint till the list of items returned is empty. Every consecutive call will have an updated packetId query param, which is the last item's packetId.

Setup: Retrofit2 with Rx adapter

Attempt:

MyApiService api = retrofit.create(MyApiService.class);
Observable<Output> call = api.myCall(packetId);
call.repeat().takeUntil(output -> output != null && !output.isEmpty())
    .compose(applySchedulers())
    .subscribe(output -> {
        packetId = output.lastPacketId();
        onFetchOutput(output)
    });

Here the packetId won't get updated, since the observable will hit with the same params it was created with.

I can always call this method again once result is fetched.

Question: Is there a reactive way of doing this? I assume that it will involve some operator which feedbacks the results somehow.

1 Answers
Related