my flow is `Flow<List<T>>`, can i conv...
# coroutines
w
my flow is
Flow<List<T>>
, can i convert it to
Flow<T>
with something like
flatten
on arrays on std lib?
b
flow.flattenConcat()
w
the docs say "Flattens the given flow of flows into a single flow" so that isn't quite right i don't think
b
you need to map lists to flows before,
flow.map { it.asFlow() }.flattenConcat()
Or
flow.flatMapConcat { it.asFlow() }
l
Copy code
fun <T> Flow<List<T>>.flattened(): Flow<T> = flow {
    collect { list ->
        list.forEach { emit(it) }
    }
}
@william The extension above can do.
w
thanks guys!
f
Hum when you do that collect in which thread is that code executed?
l
@florent One of the threads of the dispatcher running the coroutine that calls
collect
f
If the terminal operator of that stream is a collect that emit on the UI thread, would the collect of that function also be executed on the UI thread?
l
emit
can be called on a different thread if you use
flowOn