maxmello
10/25/2021, 7:28 AMviewModelScope
and lifecycleScope
both run on the Main Dispatcher. All the official and unofficial examples of these scopes together with a StateFlow
or SharedFlow
show this:
.stateIn(viewModelScope, ...) or .sharedIn(viewModelScope, ...)
. But this means that all the calculations (for example when using combine
) then run on the main thread. Wouldn’t the better default be to always run SharedFlow
and StateFlow
in viewModelScope + Dispatchers.Default
? When collecting the flow, it will run in another coroutine anyway, and UI operations can be done on the Main dispatcher.flosch
10/25/2021, 10:21 AMwithContext(Dispatcher)
.Adam Powell
10/25/2021, 1:50 PM