how do you take an Observable<Spot> which ha...
# android
l
how do you take an Observable<Spot> which has N emissions and turn it into a Single<List<Spot>> which has all N emissions?
t
You could do it using a
reducer
function to first change it from
Observable<Spot>
to
Observable<List<Spot>>
and then transform it to a
Single<List<Spot>>
e
A simple
.toList()
on a
Observable<T>
will be enough.
☝️ 1
l
.toList() turns it into a MutableList and not a List in Kotlin
e
It is simple to convert a Mutable list to a List…
l
How do you do that?
e
Also
.toList()
t
🤦‍♂️ yes, go for
toList()
p
That makes no sense. MutableList : List
e
That's why the
toList
function is just returning the same inatance typecasted to List. It does not create a new list and do all the stupid copying.
p
But you don't need kotlin's .toList
fun test(): Single<List<Int>> { return Observable.just(1, 2, 3).toList() }
If I understand correctly you suggested to call toList inside a
map
e
You're right, the toList call is not really needed.
p
I mean the issue here might be that you might remove items from the list
Observable.just(1, 2).toList().doOnSuccess { it.remove(0) }
When you call kotlin.toList it internally calls toMutableList if it has more than 1 entry.