Kotlin Concurrency: coroutineScope vs supervisorScope Explained

Welcome to another post! Today, let’s compare coroutineScope and supervisorScope in Kotlin Coroutines, something you might come across in Android interviews. We will understand the major difference between these two and try to determine when …

kotlin concurrency coroutineScope supervisorScope

Welcome to another post! Today, let’s compare coroutineScope and supervisorScope in Kotlin Coroutines, something you might come across in Android interviews. We will understand the major difference between these two and try to determine when to choose coroutineScope and when to choose supervisorScope.

coroutineScope

coroutineScope is a coroutine builder function in Kotlin Coroutines that suspends the current coroutine until all child coroutines within the scope complete or fail.

supervisorScope

supervisorScope is a coroutine builder function in Kotlin Coroutines that creates a new coroutine scope with a different error-handling strategy compared to coroutineScope. If a child coroutine within a supervisorScope encounters an exception, it doesn’t affect other coroutines within the scope, allowing them to continue executing.

Both are coroutine builder function, but did you notice the difference between the two?

coroutineScopesupervisorScope
if one child of this scope fails, it will cancel the scopeEven if any children fail, it will keep executing the other children within the scope.

You got the major difference between these two. Now let’s try to understand with some actual code examples.

coroutineScope vs supervisorScope

First, let’s set up the basics. We mainly use coroutines for asynchronous tasks, such as making API calls. I won’t write an actual API call here, but I’ll just mimic an asynchronous API call function. See the code below.

Here, I am mimicking three API calls: getConfig(), getUsers(), and getChatRooms(). The important thing to note here is that getChatRooms() will throw an Exception.

Now let’s use coroutineScope to perform the above api calls.

The above code will fail while getting rooms, and hence the config will never get fetched as config takes five seconds to fetch (check the initial setup code). So the exception is thrown before the config is completely fetched, and as it is a coroutineScope, it will stop everything as soon as the first exception is encountered.

coroutineScope vs supervisorScope

Now we will try the same thing with supervisorScope.

If you execute the above given code then you will see that even after getting the exception in getChatRooms(), the getConfig() function is executed.

You can see the above output, we got the Users first then we got the exception and finally AppConfig.

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x