Skip to content

Instantly share code, notes, and snippets.

@GauravChaddha1996
Created January 30, 2021 18:10
Show Gist options
  • Save GauravChaddha1996/064382d03905e4e52c71cb1f8a070559 to your computer and use it in GitHub Desktop.
Save GauravChaddha1996/064382d03905e4e52c71cb1f8a070559 to your computer and use it in GitHub Desktop.
class MyViewModel : ViewModel(), CoroutineScope {
override val coroutineContext =
viewModelScope.coroutineContext + Dispatchers.Default
fun someF() {
launch {
// No need to write viewModelScope.launch
// and this is automatically on Dispatchers.Default
}
}
// Since our scope uses the context from viewModelScope when it is cancelled,
// so is our custom scope and hence all
// coroutines launched inside our custom scope
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment