Coroutine Scope Functions
Explore Kotlin coroutine scope functions to manage concurrent operations in suspending functions. Understand pitfalls of common approaches, benefits of coroutineScope, and how it maintains parent-child relationships, cancellation, and proper coroutine context. This lesson helps you write safer, testable concurrent code in Kotlin.
We'll cover the following...
Imagine that we need to concurrently get data from two (or more) endpoints in a suspending function. Before we explore how to do this correctly, let’s see some suboptimal approaches.
Approaches used before coroutine scope functions
The first approach is calling suspending functions from a suspending function. The problem with this solution is that it’s not concurrent (for example, if getting data from each endpoint takes one second, a function will take two seconds instead of one).
The easiest way to make two suspending calls concurrently is by wrapping them with async. However, async requires a scope, and using GlobalScope is not a good idea.
The GlobalScope ...