viewModelScope
CoroutineScope tied to this ViewModel. This scope will be canceled when ViewModel will be cleared, i.e ViewModel.onCleared is called.
On Android
This scope is
androidx.lifecycle.viewModelScope
from AndroidX.It is bound to Dispatchers.Main.immediate.
It is created lazily, and can be accessed from any thread (it is thread-safe).
Other platforms
This scope is bound to the first available in the order: Dispatchers.Main.immediate or Dispatchers.Main.
You should make sure that you add the corresponding dependency to your project, to ensure that the kotlinx.coroutines.MainCoroutineDispatcher is available (eg. kotlinx-coroutines-swing
, kotlinx-coroutines-javafx
, ...).
It is created lazily, and can be accessed from any thread (it is thread-safe).