I'm trying to tidy up my code a little, and Single
is looking like a good choice for me as I'm doing something that will only ever emit one result.
I'm having an issue though as I was using flatMapIterable
previously to take my response (a list) and do something on each item. I'm not seeing how I can achieve this with Single.
getListOfItems()
.flatMapIterable(items -> items)
.flatMap(item -> doSomethingWithItem())
.toList()
This works fine if getListOfItems
is returning an Observable
but if I try and make it return a Single
, then I can't do flatMapIterable
and can't see an alternative, any ideas?
Another way to do it is by using
flatMapPublisher
andFlowable::fromIterable
You can convert
Single
toObservable
by using operatortoObservable()
It will look like this:
I've made something like this
flattenAsObservable should do the trick, it will map
Single
success value toIterable
(list), and emit each item of the list as anObservable
.Building on the answer from yosriz, this is what I ended up with in Kotlin
The same can be achieved using Kotlin's
map
, depending on your preference: