r1299597
r1299597

Reputation: 629

RxJava list of request

Sorry for my english. I have list of id category, and i did request like this:

for(Product cat:all_cats) {
            new DefaultMediator()
                    .getProductsInCatalog(cat.getID())
                    .timeout(15, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(list_products -> {
                                //do something
                            },
                            throwable -> {
                                Log.e("asd", "asd");
                            });
        }

i think it not good. How i can send list of request use rxJava ?

Upvotes: 2

Views: 744

Answers (1)

maheryhaja
maheryhaja

Reputation: 1687

you can achieve this by combining two operators:

fromIterable or fromArray : to create observable from all_cats

concatMap : for network call and preserve the order

the code will look like this:

Observable.fromIterable(all_cats)
            .subscribeOn(Schedulers.io())
            .concatMap(cat ->
                    new DefaultMediator()
                            .getProductsInCatalog(cat.getID())
                            .timeout(15, TimeUnit.SECONDS);

            )
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(list_products -> {
                        //do something
                    },
                    throwable -> {
                        Log.e("asd", "asd");
                    });

Upvotes: 5

Related Questions