Skip to content

Instantly share code, notes, and snippets.

@wellingtoncosta
Created February 12, 2019 15:34
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 wellingtoncosta/3a25bbd1f83167179cfe7f9b33c08b84 to your computer and use it in GitHub Desktop.
Save wellingtoncosta/3a25bbd1f83167179cfe7f9b33c08b84 to your computer and use it in GitHub Desktop.
open class CoroutineViewModel : ViewModel(), CoroutineScope {
override val coroutineContext = Main
protected val jobs = ArrayList<Job>()
infix fun ArrayList<Job>.add(job: Job) { this.add(job) }
override fun onCleared() {
super.onCleared()
jobs.forEach { if(!it.isCancelled) it.cancel() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment