Francisco Catarrinho
Francisco Catarrinho

Reputation: 63

Flatten List<Observable<X[]> into Observable<X[]>

In my project i need to make subsequent calls to an API to retrieve summaries from different courses, and its all stored in a List like so:

List<Observable<Summaries[]>> summaries

Then, i need to perform an action on those observables, and im using something along this lines:

Observable
    .switchOnNext(Observable.from(summaries))
    .doOnNext(summaries -> { actionToBeDone() })

However, this performs the action for each Observable present in the list, and in the project implementation i am only able to execute the action to all at once, so my question is:

Is it possible to

List<Observable<Summaries[]>>

into

Observable<Summaries[]>

If not, is there any workaround to this?

Upvotes: 1

Views: 62

Answers (1)

akarnokd
akarnokd

Reputation: 70017

Based on the comments, you still have to use concat for example, then collect into an ArrayList followed by a toArray mapping:

Observable.concat(summaries)
.collect(ArrayList::new, (list, summary) -> list.addAll(Arrays.asList(summary)))
.map(list -> list.toArray(new Summaries[list.size()]))
.subscribe(/* ... */);

Upvotes: 3

Related Questions