https://kotlinlang.org logo
#android
Title
# android
l

lawlorslaw

12/20/2018, 9:13 AM
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

Tobi

12/20/2018, 9:32 AM
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

edwardwongtl

12/20/2018, 9:36 AM
A simple
.toList()
on a
Observable<T>
will be enough.
☝️ 1
l

lawlorslaw

12/20/2018, 9:55 AM
.toList() turns it into a MutableList and not a List in Kotlin
e

edwardwongtl

12/20/2018, 9:56 AM
It is simple to convert a Mutable list to a List…
l

lawlorslaw

12/20/2018, 9:57 AM
How do you do that?
e

edwardwongtl

12/20/2018, 9:58 AM
Also
.toList()
t

Tobi

12/20/2018, 9:59 AM
🤦‍♂️ yes, go for
toList()
p

Paul Woitaschek

12/20/2018, 7:13 PM
That makes no sense. MutableList : List
e

edwardwongtl

12/20/2018, 7:17 PM
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

Paul Woitaschek

12/20/2018, 7:18 PM
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

edwardwongtl

12/20/2018, 7:20 PM
You're right, the toList call is not really needed.
p

Paul Woitaschek

12/20/2018, 7:22 PM
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.
2 Views