hey in the documentation SharedFlow.flowOn(...) ha...
# coroutines
r
hey in the documentation SharedFlow.flowOn(...) has no affect if I understand it correctly . So how do you switch dispatchers when collecting from the shared Flow?
l
withContext
on the collector side, simply.
đź’Ż 1
z
Are you trying to switch the dispatcher on which you’re collecting, or the dispatcher that some other upstream logic is running on? If the former,
flowOn
will not do that even for non-state flows. Louis’
withContext
is the solution there too. If the latter, there’s no way to do that generally for
StateFlow
because the only implementation of
StateFlow
that should be used is
MutableStateFlow
, which doesn’t have the concept of operating on any particular dispatcher (since it’s effectively just an observable value holder).
đź‘Ť 1
l
One can also add a dispatcher to the scope passed to
shareIn
to control where the emitter runs by default.
r
Copy code
val _aFlow = MutableSharedFlow<Int>()
val aFlow: Flow<In>
get() = _aFlow



aFlow
.onEach { …}
flowOn(<http://Dispatchers.IO|Dispatchers.IO>}.   <— This won't have effect ?
onEach{…Complex commutation } 
.launchIn(SomeScope)
So that wont have effect?
l
Is has an effect for
onEach
As the documentation of
flowOn
states quite clearly.