https://kotlinlang.org logo
#coroutines
Title
# coroutines
r

rkeazor

11/13/2020, 5:10 AM
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

louiscad

11/13/2020, 6:45 AM
withContext
on the collector side, simply.
💯 1
z

Zach Klippenstein (he/him) [MOD]

11/13/2020, 3:47 PM
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

louiscad

11/13/2020, 4:17 PM
One can also add a dispatcher to the scope passed to
shareIn
to control where the emitter runs by default.
r

rkeazor

11/15/2020, 5:19 PM
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

louiscad

11/15/2020, 5:32 PM
Is has an effect for
onEach
As the documentation of
flowOn
states quite clearly.
6 Views