fun <T> Flow<T>.launchIn(scope: CoroutineScope): Job

Terminal flow operator that launches the collection of the given flow in the scope. It is a shorthand for scope.launch { flow.collect() }.

This operator is usually used with onEach, onCompletion and catch operators to process all emitted values handle an exception that might occur in the upstream flow or during processing, for example:

.onEach { value -> updateUi(value) }
.onCompletion { cause -> updateUi(if (cause == null) "Done" else "Failed") }
.catch { cause -> LOG.error("Exception: $cause") }

Note that resulting value of launchIn is not used the provided scope takes care of cancellation.


common source
Link copied to clipboard