API calls should be made ~30 times which only differ by one parameter:
https://api.website.com/getProducts?category_id=10
https://api.website.com/getProducts?category_id=11
These calls responds are limited by 100 products per call. If a category has more products, I need to append a offset parameter. The following call will give me the subset from 101-200. A total is also provided in the response so I know when to stop.
https://api.website.com/getProducts?category_id=10&offset=100
https://api.website.com/getProducts?category_id=10&offset=200 <--category has 260 products, I stop here.
I can make the initial calls (no offset) and n-offset calls with Retrofit easily. But from Retrofit I can't register any callbacks when all calls are finished nor return the data as a list to update my UI just once.
So I like to try it with RxJava2. I accomplished the same as with Retrofit already (returning a Observable after each parent call or call with offset).
private Observable<SearchResponse> search(int category, int offset) {
Observable<SearchResponse> call = retrofitSearchService.search(category, offset);
return call.flatMap(new Function<SearchResponse, ObservableSource<?>>() {
@Override
public ObservableSource<SearchResponse> apply(@NonNull SearchResponse searchResponse) throws Exception {
if (searchResponse.getTotal() > (searchResponse.getOffset() + searchResponse.getLimit())) {
search(category, searchResponse.getOffset() + 100); <--recursion here
}
return Observable.just(searchResponse);
}
}).toList().toObservable()
.cast(SearchResponse.class);
}
Returning every response as one makes my UI update like crazy (Android livedata).
I still like to:
- return a full list of all categories and children if they have any.
- get one callback when all requests are made.
This looks promising (How To Do Recursive Observable Call in RxJava?). But I can't wrap my head around it.
If you can, please disperse of lambda. Function, Consumer, Subscriber gives me more clarity. Great!