Android – make a call using Rx, and then make multiple parallel calls from the result of the first call

I must call the API that returns the list of items. For each item in this list, I must call another API (if the list returns 8 items, I will have to make 8 parallel calls)

I finally have to return a list, which I will create using the results of these 8 parallel calls

How can I do this with rxjava? I think I have to use flatmap to convert the result of the first call to the observables list, and then I have to use the zip operator for parallel calls, but I'm not sure

Please note that I use rxjava 2 without Lambdas expressions

thank you!

resolvent:

You can do this. For example, defer () allows you to get data only at the time of subscription, and then create an observable that sends out all items (one by one) in the item list. Then flatmap () will create an observable to get the data of each item. Now you will have an observable that sends out the data object. To collect it, you can use tolist () to send out a single object (a list), It will contain all the data extracted by each observable

Note that for parallel execution, it is important that fetchdatafromitem () will subscribe to schedulers. IO (), even if it subscribes to all streams on Io

Observable.defer(new Callable<ObservableSource<Item>>() {
        @Override
        public ObservableSource<Item> call() throws Exception {
            List<Item> items = getItems();
            return Observable.fromIterable(items);
        }
    })
            .flatMap(new Function<Item, ObservableSource<Data>>() {
                @Override
                public ObservableSource<Data> apply(@NonNull Item item) throws Exception {
                    return fetchDataFromItem(item);
                }
            })
            .toList()
            .subscribe(new Consumer<List<Data>>() {
                @Override
                public void accept(@NonNull List<Data> objects) throws Exception {
                //do something with the list of all fetched data
                }
            });

to update:

If the extracted item is already observable, defer() can be replaced by flatmapiterable(), which accepts a single item list and converts it to observable of multiple items:

getItemsObservable()
        .flatMapIterable(new Function<List<Item>, Iterable<Item>>() {
            @Override
            public Iterable<Item> apply(@NonNull List<Item> items) throws Exception {
                return items;
            }
        })
        .flatMap(new Function<Item, ObservableSource<Data>>() {
            @Override
            public ObservableSource<Data> apply(@NonNull Item item) throws Exception {
                return fetchDataFromItem(item);
            }
        })
        .toList()
        .subscribe(new Consumer<List<Data>>() {
            @Override
            public void accept(@NonNull List<Data> objects) throws Exception {
                //do something with the list of all fetched data
            }
        });

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>