julioyg
11/13/2019, 10:45 AMcollect
is executed.
I'm trying:
launch(UI) {
flow.collect {
// i was hoping this was executed in UI
}
should that be running in the UI Dispatcher?nrobi
11/13/2019, 10:51 AMkotlinx.coroutines.Dispatchers.Main
for the UIsikri
11/13/2019, 10:55 AMflow
.onEach { doSmth(it) }
.launchOn(Dispatcher.Main)
julioyg
11/13/2019, 11:27 AMnrobi
11/13/2019, 11:28 AMlaunchOn()
calls collect()
sikri
11/13/2019, 11:28 AMjulioyg
11/13/2019, 11:29 AMsikri
11/13/2019, 11:30 AMlaunch(UI) {
flow.collect {
too many scopes
}
}
@ExperimentalCoroutinesApi // tentatively stable in 1.3.0
public fun <T> Flow<T>.launchIn(scope: CoroutineScope): Job = scope.launch {
collect() // tail-call
}
tseisel
11/13/2019, 2:24 PMcollect
, the passed block invoked for each collected item, and * always in the context of the current coroutine*.
Note that UI
is deprecated in favor of Dispatchers.Main
.
If you want some flow operations to be executed on another Thread/Dispatcher, you could use the flowOn
operator. Note that unlike Rx `subscribeOn`/`observeOn`, flowOn
is only applied to upstream operations.julioyg
11/13/2019, 2:25 PM