RxJava flatMapIterable with a Single
You can convert Single
to Observable
by using operator toObservable()
It will look like this:
getListOfItems()
.toObservable()
.flatMapIterable(items -> items)
.flatMap(item -> doSomethingWithItem())
.toList()
Building on the answer from yosriz, this is what I ended up with in Kotlin
getListOfItems()
.flattenAsObservable { it }
.flatMap { doSomethingWithItem(it) }
.toList()
The same can be achieved using Kotlin's map
, depending on your preference:
getListOfItems()
.map { items ->
items.map {
doSomethingWithItem(it)
}
}
flattenAsObservable should do the trick, it will map Single
success value to Iterable
(list), and emit each item of the list as an Observable
.
getListOfItems()
.flattenAsObservable(new Function<Object, Iterable<?>>() {
@Override
public Iterable<?> apply(@NonNull Object o) throws Exception {
return toItems(o);
}
})
.flatMap(item -> doSomethingWithItem())
.toList()