...

/

Coroutine Scope Functions

Coroutine Scope Functions

Learn about the coroutine scope function.

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).

Press + to interact
// Data loaded sequentially, not simultaneously
suspend fun getUserProfile(): UserProfileData {
val user = getUserData() // (1 sec)
val notifications = getNotifications() // (1 sec)
return UserProfileData(
user = user,
notifications = notifications,
)
}

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.

Press + to interact
//DOING THIS IS NOT RECOMMENDED AT ALL
suspend fun getUserProfile(): UserProfileData {
val user = GlobalScope.async { getUserData() }
val notifications = GlobalScope.async {
getNotifications()
}
return UserProfileData(
user = user.await(), // (1 sec)
notifications = notifications.await(),
)
}

The ...