Skip to content

Instantly share code, notes, and snippets.

@RBusarow
Last active July 8, 2019 01:02
Show Gist options
  • Save RBusarow/7a13554dcebcbf2bcedb2bdb12b10f88 to your computer and use it in GitHub Desktop.
Save RBusarow/7a13554dcebcbf2bcedb2bdb12b10f88 to your computer and use it in GitHub Desktop.
Example of creating a new coroutine, using withContext to modify its CoroutineContext
class SomeRepository(private val api: SomeApi) {
// note that the class doesn't have a CoroutineScope or CoroutineContext
suspend fun getSomethingExpensive() = withContext(Dispatchers.IO) {
// regardless of the caller, this coroutine will execute
// with the IO dispatcher.
api.getTheWindsOfWinter()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment