samuel
11/23/2020, 12:41 PMsuspend inline fun <T>Flow<T>.fireAndForget(crossinline block: (T) -> Unit): Flow<T> {
coroutineScope {
launch {
runCatching {
collect {
block(it)
}
}
}
}
return this
}
flosch
11/23/2020, 1:08 PMfun <T> Flow<T>.fireAndForget(
block: (T) -> Unit
): Flow<T> = onEach { emission -> runCatching { block(emission) } }
should do the tricksamuel
11/23/2020, 1:11 PMbezrukov
11/23/2020, 1:57 PMonEach
doesn't collect anything, you still have to run launchIn(someScope)
after itsamuel
11/23/2020, 2:13 PMonEach
it runs unsafeTransform
which collects the flow and applies the given transformation, so launchIn
is not necessary especially if this extension is run as an intermediary step (before the flow is emitted downstream)bezrukov
11/23/2020, 2:14 PMsamuel
11/23/2020, 2:17 PM