Skip to content

Instantly share code, notes, and snippets.

@RBusarow
Created July 22, 2019 16:47
Show Gist options
  • Save RBusarow/aeb46e0da17ce9947c985e9062e28203 to your computer and use it in GitHub Desktop.
Save RBusarow/aeb46e0da17ce9947c985e9062e28203 to your computer and use it in GitHub Desktop.
Example of layering CoroutineContext
class SomeClass : CoroutineScope {
val jobA = Job()
override val coroutineContext: CoroutineContext
get() = jobA + Dispatchers.Main
fun doSomething() {
// create a new coroutine using jobA and Main dispatcher
launch {
val jobB = Job()
// jobB and Main dispatcher
launch(jobB) {
// jobB and Default dispatcher
launch(Default) { }
}
// jobA and Default dispatcher
launch(Default) { }
// jobB and Default dispatcher
launch(jobB + Default) { }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment