Hi there, trying to work out why a mutableStateFlo...
# coroutines
m
Hi there, trying to work out why a mutableStateFlow.value can be set from outside of a coroutine, I assume it was essentially a wrapper for emit(). Could anyone help me understand what im missing? Thanks, Michael
y
I believe value read and writes are non-blocking and thread safe to use from any concurrent threads. The flows reading from it, will have their own coroutine context. MutableStateFlow also conflates updates, so slow consumers only see the last value. The docs cover a lot of the motivation and implementation details https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-mutable-state-flow/index.html
j
It's not exactly like
emit
because it can be done without interaction with the subscribers. Basically there is no backpressure thanks to the conflation, so setting the value can always be done without suspending, and collectors are notified asynchronously because they check themselves for value updates. The implementation is not simple to be honest
m
Great thank you didn’t notice the conflation and this statement helps: a slow collector skips fast updates, but always collects the most recently emitted value