Skip to content

Instantly share code, notes, and snippets.

@RBusarow
Last active July 8, 2019 00:38
Show Gist options
  • Save RBusarow/6627bf756bf7ec6b5abce4b0d993d16a to your computer and use it in GitHub Desktop.
Save RBusarow/6627bf756bf7ec6b5abce4b0d993d16a to your computer and use it in GitHub Desktop.
Example of overriding ContinuationInterceptor in coroutine builders
class SomeClass : CoroutineScope {
val job = Job()
// set the default dispatcher to Dispatchers.Main
override val coroutineContext = job + Dispatchers.Main
fun updateSomething() = launch(Dispatchers.Default) {
// this coroutine overrides the CoroutineScope's ContinuationInterceptor
// and uses Dispatchers.Default
val result = getSomethingExpensive()
launch(Dispatchers.Main) {
// this coroutine inherits Default as the ContinuationInterceptor
// and needs to explicitly switch back to Main
// in order to update the UI
someView.text = result
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment