Why must "Dispatchers.Main" be added to the root job of an implementation of an Activitys CoroutineScope?

Answer #1 100 %

When you wrote this:

launch(Dispatchers.Main) {
    try {
    } catch (e: Exception) {
        // e will be a JobCancellationException if the activty is destroyed

you may not have realized that launch is actually invoked with your ScopedAppActivity as the receiver. So you effectively wrote

this.launch(Dispatchers.Main) { ... }

launch is an extension function on CoroutineScope and it will use its coroutineContext as the starting point, combining it with whatever you specify in the parentheses. So, in your case, the effective context is

job + Dispatchers.Main + Dispatchers.Main

As you can imagine, this is equal to

job + Dispatchers.Main

so when you remove Dispatchers.Main from your coroutineContext, nothing changes.

So what is the reason for Dispatchers.Main?

The advantage of providing Dispatchers.Main in coroutineContext is that you don't have to supply it every time, so you can just write

launch { ... }

and the block inside launch will stay on the GUI thread, which is the most natural way to use coroutines on Android and other GUI applications.

Why is Dispatchers.IO not added, too?

Since that line is not about declaring all the dispatchers you'll use, but the default one, it doesn't make sense to provide more than one.

On another level, CoroutineContext isn't a list (which is kind of implied by the + operator), but a map. The + syntax works because each object you add declares its own map key, which + uses to put it into the context's internal map. So it's actually impossible to put two dispatchers into one CoroutineContext.

You’ll also like:

© 2023 CodeForDev.com -