Skip to content

Instantly share code, notes, and snippets.

@PhilippeBoisney
Last active February 12, 2019 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhilippeBoisney/3da4e7c0262123d0cc4ed38db71d3c85 to your computer and use it in GitHub Desktop.
Save PhilippeBoisney/3da4e7c0262123d0cc4ed38db71d3c85 to your computer and use it in GitHub Desktop.
abstract class BaseViewModel: ViewModel() {
/**
* This is a scope for all coroutines launched by [BaseViewModel]
* that will be dispatched in a Pool of Thread
*/
protected val ioScope = CoroutineScope(Dispatchers.Default)
/**
* Cancel all coroutines when the ViewModel is cleared
*/
override fun onCleared() {
super.onCleared()
ioScope.coroutineContext.cancel()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment