safeLaunch
context(viewModel: ViewModel)
fun safeLaunch(context: CoroutineContext = EmptyCoroutineContext, errorEvents: MutableSharedFlow<UiText>? = null, tag: String? = null, block: suspend CoroutineScope.() -> Unit): Job(source)
fun safeLaunch(context: CoroutineContext = EmptyCoroutineContext, errorEvents: MutableSharedFlow<UiText>? = null, tag: String? = null, block: suspend CoroutineScope.() -> Unit): Job(source)
Launches a coroutine in viewModelScope that catches all exceptions except CancellationException. Non-cancellation errors are logged and emitted to errorEvents (if provided) for one-shot UI consumption (e.g. snackbar / toast).
Parameters
context
optional CoroutineContext element (typically a dispatcher) merged into the launch. Defaults to EmptyCoroutineContext, inheriting viewModelScope's dispatcher.
// In a ViewModel:
safeLaunch(errorEvents = _errors) {
repository.saveData(data)
}Content copied to clipboard