I have rxjava3 Flowable
having Item
objects and want to run async operation in batches only for elements matching a condition. Then I want to allow downstream operators to iterate over original Item objects stream in the original order.
Flowable<Item> source ...
Flowable<Item> updated = source.filter(i -> i.isCondition())
.buffer(3)
.flatMap(batch -> {
return apiCall(batch).flatMapPublisher(apiRes -> {
for (Item item : batch) {
item.updateFrom(apiRes)
}
return Flowable.fromIterable(batch);
}
}
// return Flowable from source in the same order
// but only after updated completed for the batch
// or isCondition is false
Lets say I have list of ids and for ids bigger than 10 I want to get group name using remote service accepting list of Ids.
5
4
12
3
15
6
It should call getGroupIds(12, 15) and return:
5, null
4, null
12, sport
3, null
15, music
6, null
What is best option to achieve it for large data set? The error in apiCall should terminate the source flowable.
There is a potential of large number of elements between 12 and 15 in above example - it will increase size of buffer used to keep original order and thus it should call apiCall if i.isCondition() is true for one element and false for following n elements to let buffer to flush to the output after apiCall gets processed. It can be simplified by using source.buffer(1000) before filter gets applied.
buffer()
. It sounds like a simplemap()
call would be enough to either returnnull
when the condition is not met and the actual API call result, when the condition is met.