Kotlin Coroutines: Getting Started in Android
By: Rahul Pandey
Tags: Kotlin Coroutines, Kotlin, Asynchronous, Android, Coroutine

Kotlin coroutines are the best way to handle asynchronous programming on Android. Google has made it clear that coroutines are the path forward for doing expensive, long-running operations in your app, so you can expect to see more libraries and APIs start to adopt these concepts.

Android apps run as a single process on your phone’s OS. A process can contain many threads: it will have a special main thread, and it can also have many background threads. By default, your application code will run on the main thread, also called the UI thread because that’s where the Android system will draw your app’s UI 60 times per second.

The main (UI) thread will attempt to draw your app at 60 times/sec, or 1 frame every 16ms

It’s really important that any of the code you write doesn’t interfere with the Android system’s ability to draw your app at the refresh rate of 60 times/sec.

That’s why network calls to an API, reading/writing a file from a database, and other expensive operations are done in a background thread. This is called asynchronous programming to indicate that this is not happening synchronously on the main thread, and instead this work is being punted to the background so that we can avoid blocking the main thread and avoid something called jank.

The complexity comes from the fact that anything which updates the UI must be run on the UI thread. For example, if we make an API request, we’ll do the networking on the background thread, but there has to be some mechanism to send the result back to the main thread so we can update the UI with it.

Traditionally, Android has solved this problem with callbacks, where you pass in a function as a parameter, and that function is invoked when the expensive operations has finished. Callbacks have several drawbacks: they require a new programming model, demand repeated error handling, and often come with nested callbacks, i.e. callback hell. On the other hand, coroutines allow us to combine the simplicity of synchronous code with the power of asynchronous code.

What is a coroutine?

You can think of a coroutine like a lightweight thread, but one key difference is that a coroutine can be suspended without blocking the thread it is running in. Many coroutines can exist in the same thread. You can update the function signature with suspend, which means that function can only be called from a coroutine or another suspend function.

In order to create a coroutine, you will typically use an extension function on CoroutineScope, or use a coroutine scope coming from the Android framework (e.g. ViewModel or LifecycleOwner scope). In either case, you have the option to specify the dispatcher where the coroutine should run- see the below diagram for an idea of the uses for the Default, Main, and IO dispatchers.

Various built-in dispatchers for Kotlin coroutines

Another concept we discuss in the video is main safety, which means that it should always be safe to launch the coroutine from the main thread. The responsibility of changing the coroutine context or using the correct dispatcher lies with the function/library.

You can find the code I wrote for the demo app here: https://github.com/rpandey1234/Coroutines

Recommended further reading and watching: