Last active
July 8, 2019 00:38
-
-
Save RBusarow/6627bf756bf7ec6b5abce4b0d993d16a to your computer and use it in GitHub Desktop.
Example of overriding ContinuationInterceptor in coroutine builders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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