elnino
elnino

Reputation: 245

Combination FlatMap and SwitchMap

I have specific requirement. I have subject as source ids of users. I need use id for downloading data of user. But when I receive same id of user later I need cancel old subscription (when was not finished) and subscribe new Observable. I need switchMap operator for each id, but switchMap is only for one value. And I want to downloading data of all users simultaneously (concurrency) - flatMap.

Upvotes: 1

Views: 239

Answers (1)

akarnokd
akarnokd

Reputation: 69997

Use groupBy to split along ids, use switchMap on each group when doing flatMap:

ids.groupBy(v -> v)
.flatMap(g -> g.switchMap(id -> download(id)), false, Integer.MAX_VALUE)

(MAX_VALUE is there in case there are more groups than the default parallelism.)

Upvotes: 3

Related Questions