Skip to content

Instantly share code, notes, and snippets.

@RBusarow
Last active April 20, 2019 13:14
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 RBusarow/3d4d8b4698afe4a6a26359db03964cab to your computer and use it in GitHub Desktop.
Save RBusarow/3d4d8b4698afe4a6a26359db03964cab to your computer and use it in GitHub Desktop.
CoroutineScope class delegation
abstract class BaseViewModel :
ViewModel(),
CoroutineScope by SupervisorCoroutineScope(Dispatchers.Default) {
override fun onCleared() {
cancel()
super.onCleared()
}
}
abstract class BaseActivity :
Activity(),
CoroutineScope by MainScope() {
override fun onDestroy() {
cancel()
super.onDestroy()
}
}
// this is dangerous since the class doesn't have a lifecycle!
class MyRepositoryImpl :
MyRepository,
CoroutineScope by SupervisorCoroutineScope(Dispatchers.IO)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment