Skip to content

Instantly share code, notes, and snippets.

// combines flowwithlifecycle, oneach and launchin
// warning: don't use inside coroutines!
fun <T> Flow<T>.launchOnEach(
owner: LifecycleOwner,
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
action: suspend (T) -> Unit,
): Job = flowWithLifecycle(owner.lifecycle, minActiveState)
.onEach(action) // order matters here, see doc
.launchIn(owner.lifecycle.coroutineScope)