Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Last active November 10, 2020 11:17
Show Gist options
  • Save manuelvicnt/6631488851c957354a60f0f10b4f442f to your computer and use it in GitHub Desktop.
Save manuelvicnt/6631488851c957354a60f0f10b4f442f to your computer and use it in GitHub Desktop.
private const val JOB_KEY = "androidx.lifecycle.ViewModelCoroutineScope.JOB_KEY"
val ViewModel.viewModelScope: CoroutineScope
get() {
val scope: CoroutineScope? = this.getTag(JOB_KEY)
if (scope != null) {
return scope
}
return setTagIfAbsent(JOB_KEY,
CloseableCoroutineScope(SupervisorJob() + Dispatchers.Main.immediate))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment