Skip to content

Instantly share code, notes, and snippets.

@RBusarow
Last active April 20, 2019 12:45
Show Gist options
  • Save RBusarow/55dda437b4c64d92cb7bd363966760a1 to your computer and use it in GitHub Desktop.
Save RBusarow/55dda437b4c64d92cb7bd363966760a1 to your computer and use it in GitHub Desktop.
Basic boilerplate version of a CoroutineScope implementation in a ViewModel, with an abstract BaseViewModel
class MyViewModel : BaseViewModel() // nothing at all to implement!
abstract class BaseViewModel : ViewModel(), CoroutineScope {
private val job = SupervisorJob()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Default
override fun onCleared() {
job.cancel()
super.onCleared()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment