Coroutines – Dispatcher & Yield
Today another day in quarantine since the COVID-19 outbreak. So I thought let’s really learn how coroutines work instead of just using it and hope for the best. I did the codelab from Google that did a pretty good job for me to understand how it works.
I wanted to highlight how to dispatch a coroutine and how it could be shorter then you think.
To switch between any dispatcher, coroutines uses withContext. Calling withContext switches to the other dispatcher just for the lambda then comes back to the dispatcher that called it with the result of that lambda.
By default, Kotlin coroutines provides three Dispatchers: Main, IO, and Default. The IO dispatcher is optimized for IO work like reading from the network or disk, while the Default dispatcher is optimized for CPU intensive tasks.
This code doesn’t support coroutine cancellation, but it can! Coroutine cancellation is cooperative. That means your code needs to check for cancellation explicitly, which happens for you whenever you call the functions in kotlinx-coroutines.
Because this withContext block only calls blocking calls it will not be cancelled until it returns from withContext.
To fix this, you can call yield regularly to give other coroutines a chance run and check for cancellation. Here you would add a call to yield between the network request and the database query. Then, if the coroutine is cancelled during the network request, it won’t save the result to the database.
You can also check cancellation explicitly, which you should do when making low-level coroutine interfaces.
Both Room and Retrofit use a custom dispatcher and do not use Dispatchers.IO. Room will run coroutines using the default query and transaction Executor that’s configured. Retrofit will create a new Call object under the hood, and call enqueue on it to send the request asynchronously.
You do not need to use withContext to call main-safe suspending functions. Since both Room and Retrofit provide main-safe suspending functions, it’s safe to orchestrate this async work from Dispatchers.Main.
By convention, you should ensure that suspend functions written in your application are main-safe. That way it is safe to call them from any dispatcher, even Dispatchers.Main.

This was a delight to read. You show an impressive grasp on this subject! I specialize about Advertise and you can see my posts here at my blog UY6 Keep up the incredible work!
Great job site admin! You have made it look so easy talking about that topic, providing your readers some vital information. I would love to see more helpful articles like this, so please keep posting! I also have great posts about Airport Transfer, check out my weblog at UY3
It is a pleasure to read this weblog, thanks to its up-to-date information and interesting posts. Look into my web page YQ9 for some really good points and find out more about Online Business.
Bookmarked, so I can continuously check on new posts! If you need some details about Car Purchase, you might want to take a look at QH8 Keep on posting!