7

Kotlin Coroutines Tutorial (STABLE VERSION) – async / await, withContext, launch

With Kotlin 1.3 we finally have a stable library for coroutines. This means that there won’t be any breaking changes to the API. Now is an especially good time to learn how to use them.

Coroutines are basically light-weight, much more efficient threads. As a bonus, they are extremely easy to work with once you know the basics. Asynchronous coroutine code looks the same as a classic synchronous code. You don’t need to learn any new programming paradigms in order to use Kotlin’s coroutines.

In this tutorial you are going to learn quite a bit about coroutines – different ways of launching them, blocking versus non-blocking code and async / await.

?Get the code from this tutorial?

Or get the code here:

 

Matej Rešetár
 

Matej is an app developer with a knack for teaching others. If he's not programming, making tutorials or doing other business, he's mostly working out, listening to audiobooks and taking cold showers.

  • Will says:

    Hi, I saw the tutorial. It is awesome. I already commented in youtube (arkorott) and recommended your tutorial on stack overflow.

    But I am still stuck. I realized that by doing this in the code below, I was creating a new thread but still blocking the main thread.

    fun getCrimes(): ArrayList = runBlocking(Dispatchers.Default) {
    val result = async { crimesDAO.getAllCrimes() }.await()
    [email protected] result as ArrayList
    }

    How do I have a coroutine run on a separate thread but get the result back on the main thread when finished?
    I asked this on Stack Overflow but have not yet solved it:

    https://stackoverflow.com/questions/52746866/android-room-kotlin-query-in-background-thread-return-value-problem/52752174#52752174

    Any help much appreciated !!
    Thanks

    • Did you resolve your issue with what I wrote you on YouTube?

      • Will says:

        Not yet. Question.
        If for example I make a suspend function like this:

        suspend fun retrieveCrimes(): List {
        return crimesDAO.getAllCrimes() // This is the actual call to the DAO
        }

        How can I call it without blocking the main thread. What I tried (copied below) creates a new thread but blocks the calling one, so it defeats the purpose.

        fun getCrimes(): ArrayList = runBlocking(Dispatchers.Default) {
        val result = async { retrieveCrimes() }.await()
        [email protected] result as ArrayList
        }

        I still want to get the result but do not want to block the main thread

      • Will says:

        Not yet. Question.
        If for example I make a suspend function like this:

        suspend fun retrieveCrimes(): List {
        return crimesDAO.getAllCrimes() // This is the actual call to the DAO
        }

        How can I call it without blocking the main thread. What I tried (copied below) creates a new thread but blocks the calling one, so it defeats the purpose.

        I still want to get the result but do not want to block the main thread

  • I believe async blocks are not supported in Kotlin 1.3

  • Jose Caballero says:

    I love the time you take to make these clear and friendly examples.
    Once I had a mentor that said that smart people usually do not know how to teach and the reason is that they see things as they were easy as it is for them… I am not saying you are not smart, I am sure you are incredibly smart, what I am saying is that you truly know how to teach, keep that great job please, the world needs more Matejs

    • Thank you very much, and thanks for learning here! I have a different thing in mind, it’s supposedly a quote by Einstein “If you can’t explain it simply, you don’t understand it well enough.”

  • >