Quick Introduction to Kotlin Coroutines, Part 1

Published Dec 18, 2017Last updated Feb 23, 2018
Quick Introduction to Kotlin Coroutines, Part 1

Motivation

First, let me start this article by saying that I spent a few years developing Android apps in Java, and the one thing that I used to do frequently was separate layers of abstraction with interfaces.

For example, in the networking layer, I end up writing lots of interfaces that declare methods, with a listener parameter, and this listener will implement some onSuccess and onFail methods. What this leads to is lots of nested callbacks where I go a few layers deep into callbacks and onSuccess and onFail blocks, something like the code below.

interface RESTClient{
    void getUsersList(RESTListener<List<User>> listListener);
    void createUser(CreateUserForm userForm, RESTListener<User> listener);
    void updateUser(String userId, CreateUserForm userForm, RESTListener<User> listener);
    void getUserDetails(String userId, RESTListener<User> listener);
    void deleteUser(String userId, RESTListener<String> listener);
}
 interface RESTListener<T>{
 	void operationFailed(String reason);
    void operationSuccess(T object);
}

Then Kotlin came along, was supported by Google officially, and suddenly, everyone went mad, and for good reason. It is a modern Swift-like, compiled, language, which is a huge step up from the Java world.

One thing caught my eye when I first read through the Kotlin documentation, Kotlin coroutines. I thought, well this for me, the one thing I can use right now to avoid my callback hell problem. So I jumped straight into it.

Here is my journey to understanding what Kotlin coroutines are.

Asynchronous code as a synchronous code

The header above is the essence of what coroutines are, it is a way to declare a block of code in which every line suspends execution of the block.

What does that mean? Well, you simply don’t have to provide a callback for long-running functions. You just add that function to a coroutine and the coroutine will suspend its execution until that method returns, hence no more callbacks!

Look at the code below:

interface DataStorageContract {
    suspend fun storeValue(key: String, value: String)
    suspend fun storeValue(key: String, value: Int)
    suspend fun getStringValue(key: String): String
    suspend fun getIntValue(key: String): Int
}

This is my interface for the data storage contract in Kotlin, no listener interface needed. You will notice the suspend keyword in front of all methods — that is a special Kotlin syntax to indicate that this function should be run from a coroutine block or another suspend function.

Basically anything that involves a network call or a CPU intensive task should be placed inside suspend functions and then consumed from a coroutine block.

Coroutine builders

If we want to call our suspend function, we need to pass it to the coroutine builder, which will do the heavy lifting for us, and ensure that the code inside that builder will run on a thread of our choice.

For example, let’s use launch coroutine builder:

launch {
    // some code
    val value = dataStorageImplementation.getStringValue("some_key")
    // some code
}

That’s it, you simply consume your suspend function inside the launch block and you are good to go.

Every time a suspend function is encountered, the execution of this coroutine block suspends, waiting for the function to return. Any code above our getStringValue method call will be executed first, then our function, then the rest of code below, no callbacks!

So on which thread will the above launch block execute? Obviously, we want it to execute on a background thread and we want to update the UI with values from our suspend functions.

Coroutine context

Every coroutine builder can have a context in which to run, including the main thread or some other thread. If no context is specified, like the example above, it’s the CommonPool of threads, meaning it will run on a separate thread from the main thread of the app.

So if the code above is running in the background thread, how do you update the UI? To update the UI, there is one more step that needs to be done, and it's another launch block with a coroutine context specified, like the example below:

launch {
    // some code
    val value = dataStorageImplementation.getStringValue("some_key")
    // some code
    launch (UI) {
        textView.text = value
    }
}

Everything inside the second launch will use the main thread event dispatcher. That will post our update on the main thread. What is cool about this is that the second launch block will also wait for the code above to execute. Once it does execute, the value variable will be set and we can safely update our UI. Coroutine context is simply a coroutine dispatcher, which controls on what thread the coroutine block is running.

Coroutines are lightweight threads

Coroutines run on threads and can suspend its execution without blocking the thread it runs on, so the thread execution is not tied to the coroutine execution — they operate separately.

If you think about it, callbacks are almost the same thing. They suspend execution and continue once the result is available, but have a boilerplate syntax and requires you to repeat yourself everytime you want to suspend an execution.

Launch vs Run coroutine builder

One problem with nesting coroutines is that every coroutine block will have its own execution line, meaning the second coroutine block will not suspend the parent coroutine. There is a better way of writing the code above, since getStringValue function returns a value.

We can use a run coroutine builder to return a value from a suspend function, hence we can start on the main thread and invoke our getStringValue function inside a background thread, or the other way around from the example above.

launch(UI) {
    val value = run(CommonPool) {
        dataStorageImplementation.getStringValue("some_key")
    }
    textView.text = value
}

The main difference between launch and run coroutine builder is the return value. Launch has none while run has, so in this case, we will assign our variable value to whatever the result value of the run block is.

I specified a dispatcher for run block intentionally, since nested coroutines take the parent coroutine context if not specified. We do not want that in this case — we want the run block to execute on a background thread.

I believe this is all you need to know to start playing around with coroutines. Don’t hesitate to open Android Studio and write some coroutines!

Further reading

Coroutines can do much more if you need it. There are many different coroutine builders, channels, synchronization options, etc. The best introduction to coroutines is the official Kotlin coroutines guide on GitHub. Don’t hesitate to read it — you will find lots of gems in there.

Dependencies

To use coroutines in your project, you need a couple of additions to your build grade file, mainly these two lines:

compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.19.3"
compile "org.jetbrains.kotlinx:kotlinx-coroutines-android:0.19.3"

The coroutines-android dependency is for the special UI context that can be passed to coroutine builders that use the main thread dispatcher to dispatch events on the main thread.

You then have to tell Gradle that we will use coroutines in the app by adding these lines to the app build Gradle file:

kotlin {
    experimental {
        coroutines "enable"
    }
}

GitHub sample project: Fibonacci counter

I wrote a simple Android app in Kotlin that implements a Fibonacci counter with coroutines. Check it out on GitHub, clone the repository, play with it, and most importantly, have fun!

Conclusion

This is a part one of a two-part series. Stay tuned for the next article.

Discover and read more posts from Abdurahman Adilovic
get started