4

I have a Set<Object> and for each entry in the Set I have to make an API call passing that as a param. And I have to process each of those responses & populate another Map with some own logic

Sample sequential execution:

List<MyResponse> responses = newArrayList<>();
Set<StoreNode> nodes = // Assume we have a Set
nodes.forEach(storeNode -> responses.add(myAPI.myMethod(storeNode.getId()));
responses.forEach(response -> processResponse(response, myMap); // This is a common map & I have some custom logic to populate this map

How can I achieve the same using Observables? I wanna make those calls in parallel & populate my common map myMap

I came across map(), flatMap() & zip() but most of the examples I saw were simple ones that didn't make API calls & process their response.

Narayanan P S
  • 111
  • 2
  • 6

1 Answers1

2

It depends which RxJava version you are using. If its older than 2.0.5, then you need to do flatMap, where you create another Observable and ensure things are pallel there. See this answer on StackOverflow.

Otherwise, I recommend using Flowable and then you can use parallel() operator that changes your Flowable to ParallelFlowable.

So you could do it like this:

Flowable.fromIterable(nodes)
        .parallel() // you can also specify number of rails here
        .runOn(Schedulers.computation())
        .map(node -> myAPI.myMethod(node.getId()))
        .sequential()
        .subscribe(
                response -> processResponse(response, myMap),
                error -> log(error)
        );

See Parallel flows docs for more.

michalbrz
  • 3,354
  • 1
  • 30
  • 41