Jhon Yencer
Jhon Yencer

Reputation: 21

Group parallel calls in RX Java

Does anyone know how to execute the following: The content of an element must be parsed and the sub-elements must be grouped by making a parallel query You should advance to the next item in the list only if the group query was previously completed

Element element = new Element(); 
element1.get(0) = urlA
element1.get(1) = urlB
element1.get(2) = urlC

List<Element> list = new ArrayList();
list.get(0); //consult in parallel  [urlA, urlB, urlC]

//You must wait for the previous query to finish
list.get(1); //consult in parallel  [urlX, urlY, urlZ] 

I currently use rxjava-3.0.3

Upvotes: 2

Views: 92

Answers (1)

Eugene Popovich
Eugene Popovich

Reputation: 3473

You may try to use concatMap concatMap marble diagram operator

So the code may look like this

Flowable.fromIterable(list)
.concatMap(element -> Flowable.just(0, 1, 2)
  .map(index -> element.get(index))
  .observeOn(Schedulers.computation())
  .flatMap(url -> doParsingInParallel(url)))
.doOnNext(parsingResult -> handleParsingResult(parsingResult));

Upvotes: 2

Related Questions