collectSafely

abstract suspend fun collectSafely(collector: FlowCollector<T>)(source)

Accepts the given collector and emits values into it.

A valid implementation of this method has the following constraints:

  1. 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.

  2. 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.