Instructions

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Coroutines

This lesson is about coroutines. Before you start coding, you need to understand what coroutines are. A coroutine is a piece of code that can be suspended and resumed. It’s important to understand that a coroutine is not a thread. But it does run on a thread. A coroutine can be resumed on the same thread as it was suspended or on a different one. See the following image:

Fcyiez #1 Hfsuut #2 Izpew Cibaurokeq Colaixute #9 Jiceulona #2

Suspending

Suspending is a way to pause a coroutine and resume it later on. Just like you can save a game at a checkpoint. You can then go back to that checkpoint later on. You can have multiple checkpoints, and you can go back to any of them in any order.

Building Coroutines

To start your very first coroutine in your program, you need to use one of the coroutine builders. They take a lambda as an argument, describing what block of code will run inside the coroutine. The simplest example looks like this:

runBlocking {
  doSuspendableWork() // this is a suspending function
}

runBlocking

The runBlocking builder is the simplest one. It blocks the current thread until the coroutine completes. There are no advantages to suspensions in this case. During the period when the coroutine is suspended, the thread is blocked. It consumes the resources but doesn’t do any useful work.

launch

The launch builder is the one you’ll use most often. It starts a new coroutine and returns a Job object. The Job is a handle to the coroutine. You can use it to check the coroutine status, whether it’s still running, or has completed, or failed. You can also cancel the coroutine using the Job‘s cancel method. The launch call doesn’t block the caller thread.

async

The async builder is similar to the launch builder. It also requires a CoroutineScope. The major difference is that the async builder returns a Deferred object. It’s also a Job, but it has the additional ability to return a value. The async builder is useful when you need to utilize the result of the coroutine or even multiple coroutines. With async, there’s no need to use any additional storage mechanism outside the coroutine to deal with the results. The async builder also differs from the launch builder in the way it handles exceptions. You’ll learn about this in following lessons, too.

Cancelling Coroutines

To cancel coroutines, you can call the cancel method on its Job. You can also cancel the entire scope - it will call the cancel method on all its jobs. But, often, you won’t need to do that manually. If you’re using the CoroutineScope associated with the Android entity, the coroutines library will handle the cancellations related to your lifecycle. For example, the scope obtained from the rememberCoroutineScope() function in a composable will be cancelled when the composable is removed from the screen.

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo