0

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:

  1. return a full list of all categories and children if they have any.
  2. 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!

S. Gissel
  • 1,788
  • 2
  • 15
  • 32
  • this all just seems to me like you're looking for a pagination mechanism ? if so, why not use the paging library ? – a_local_nobody Jan 03 '22 at 18:43
  • 1
    Why are you making all these calls up front? That's generally an antipattern- the reason the API has a limit per call is they don't want to be sending all that data at once. Prefetch ahead of time sure, but prefetch when its likely to be needed. You aren't going to need 100+ products when showing a list of products 99% of the time. Grabbing it all upfront is just going to be horrible for performance across the board. – Gabe Sechan Jan 03 '22 at 18:44
  • @GabeSechan I like to grab all products from all categories and like to sort then eg. by price, color... And as the API doesn't support a parameter &sortBy=price I need the fetch all products upfront. No problem as most of the categories feature < 100 products most of the time API calls equal categories. – S. Gissel Jan 03 '22 at 18:50

0 Answers0