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

Coroutine Scopes

In the previous lesson, you used a scope to start a coroutine. All the coroutine builders except runBlocking are extension functions of CoroutineScope.

suspend fun fetchAndProcessData() = coroutineScope { 
  val deferred1 = async { fetchData1() }
  val deferred2 = async { fetchData2() }
  val data = awaitAll(deferred1, deferred2)
  processData(data)
}
Gmgorzawaf waqnojcetpc Uvarz daxoogipu bzuwsf or i plifu Ofqik mudgorhs wug li melcisox ul vux Pudoawubij laqj o joahapjhl Comovrw xoiz ceq yvuhmhej Culkehiqn fzu cohebt koglifl ozk jxe pqucjrec Oqxivt iq ktigvxaq rpoxitale na givuddv 6 1 7 6 8 2

Dispatching Coroutines

The dispatcher is a mechanism that determines which thread the coroutine runs on. The dispatcher is like a thread pool. There are several predefined dispatchers in the Kotlin coroutines library:

Default Dispatcher

As the name suggests, the Default dispatcher is used by coroutines builders if you specify no particular dispatcher and if the CoroutineScope you use to create coroutines doesn’t have a dispatcher. Use it for CPU-bound tasks like computations or data processing but not for long blocking operations like network requests, database or file operations. The Default dispatcher is backed by a shared pool of threads. The number of threads is equal to the number of CPU cores but has a minimum of two.

IO Dispatcher

The IO dispatcher is suitable for Input/Output tasks like network requests, filesystem, or database operations. How do such operations differ from CPU-bound tasks? IO operations are usually blocking. Reading from the disk or network takes time, a lot of time. And you usually have to read the data in one go. The CPU is much faster than the disk or network. It needs to wait for the data to arrive or depart. The CPU can do other work during that time. The IO dispatcher uses a pool of threads that grows on demand. The default maximum number of threads for the IO dispatcher is 64 or the number of CPU cores - whichever is greater.

Main Dispatcher

The Main dispatcher is the dispatcher for the main (UI) thread. It always provides the same thread. On Android platforms, it’s the Android main thread.

Unconfined Dispatcher

The Unconfined dispatcher is the dispatcher that doesn’t impose any thread. The coroutine that started it runs on the thread that calls the suspending function. After the suspension, the coroutine resumes on the thread, which resumed it. Those two threads may be different. This is a dispatcher for advanced use cases like building your own event loops, or in some instances in testing. It isn’t a part of this course.

Switching Dispatchers

You can switch dispatchers in a coroutine. The withContext function is the way to do it. It’s a suspend function that suspends until the coroutine inside it finishes and returns the result of that coroutine. Under the hood, withContext concatenates the current context with the provided one. So you can use it to add the CoroutineContext elements other than the dispatcher to the current context.

suspend fun fetchAndDisplayData() {
  val data = withContext(Dispatchers.IO) { fetchData() }
  displayData(data)
}
See forum comments
Download course materials from Github
Previous: Introduction Next: Demo