collectSafely
Accepts the given collector and emits values into it.
A valid implementation of this method has the following constraints:
It should not change the coroutine context (e.g. with
withContext(Dispatchers.IO)
) when emitting values. The emission should happen in the context of the collect call. Please refer to the top-level Flow documentation for more details.It should serialize calls to emit as FlowCollector implementations are not thread-safe by default. To automatically serialize emissions channelFlow builder can be used instead of flow
Throws
if any of the invariants are violated.