Skip to content Skip to sidebar Skip to footer

How To Make Multiple Requests With Reactive Android And Retrofit

I have presenter which calls getShops() method. Observable> observable = interactor.getData(); Subscription subscription = observable

Solution 1:

Just use Observable.range and concatMap your calls with it. If you don't know your upper range, you can use takeUntil with some condition.

Something like this:

Observable.range(0, Integer.MAX_VALUE)
            .concatMap(pageNum -> api.getShops(pageNum).map(doYourMapping))
            .takeUntil(shops -> someCondition);

If you want only one onNext call you can add this after takeUntil:

.flatMapIterable(shops -> shops)
.toList();

Solution 2:

You can achieve that with Observable.concat method.

/**
 * Returns an Observable that emits the items emitted by two Observables, one after the other, without
 * interleaving them.
*/Observable<List<Shop>> observable =Observable.concat(
            interactor.getData(0),  interactor.getData(1), interactor.getData(2) //and so on);

Your getData method should accept pageNum parameter: public Observable<List<Shop>> getData(int pageNum)

But it is only an answer for your particular question, which could not fullfill your needs. Because as i see there could be different count of pages, but concat is only for static count of observables.

You may try to solve that with building observable recursively.

Post a Comment for "How To Make Multiple Requests With Reactive Android And Retrofit"