Last active
July 8, 2019 01:02
-
-
Save RBusarow/7a13554dcebcbf2bcedb2bdb12b10f88 to your computer and use it in GitHub Desktop.
Example of creating a new coroutine, using withContext to modify its CoroutineContext
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 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