ViewModelKt
public final class ViewModelKt
Summary
Public methods |
|
|---|---|
static final @NonNull CoroutineScope |
getViewModelScope(@NonNull ViewModel receiver)The |
Public methods
getViewModelScope
public static final @NonNull CoroutineScope getViewModelScope(@NonNull ViewModel receiver)
The CoroutineScope associated with this ViewModel.
The CoroutineScope.coroutineContext is configured with:
-
SupervisorJob: ensures children jobs can fail independently of each other. -
MainCoroutineDispatcher.immediate: executes jobs immediately on the main (UI) thread. If theDispatchers.Mainis not available on the current platform (e.g., Linux), we fallback to anEmptyCoroutineContext.
This scope is automatically cancelled when the ViewModel is cleared, and can be replaced by using the ViewModel constructor overload that takes in a viewModelScope: CoroutineScope.
For background execution, use kotlinx.coroutines.withContext to switch to appropriate dispatchers (e.g., kotlinx.coroutines.IO).
| See also | |
|---|---|
onCleared |