Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update task.md #16

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Coroutines/Outer scope/task.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
When you start new coroutines inside the given scope, it's much easier to ensure that all of them run with the same
context. It is also much easier to replace the context if needed.
When you start new coroutines within a given scope, it becomes much easier to ensure that all of them run within the same
context. It also simplifies the process of replacing the context if needed.

Now it's time to learn how using the dispatcher from the outer scope works. The new scope created by
Now, it's time to learn how utilizing the dispatcher from the outer scope works. The new scope created by either
the `coroutineScope` or by the coroutine builders always inherits the context from the outer scope. In this case, the
outer scope is the scope the `suspend loadContributorsConcurrent()` function was called from:

Expand All @@ -12,8 +12,8 @@ launch(Dispatchers.Default) { // outer scope
}
```

All of the nested coroutines are automatically started with the inherited context. The dispatcher is a part of this
context. That's why all of the coroutines started by `async` are started with the context of the default dispatcher:
All nested coroutines are automatically started with the inherited context, of which the dispatcher is a part.
That's why all of the coroutines instigated by `async` are started within the context of the default dispatcher:

```kotlin
suspend fun loadContributorsConcurrent(
Expand All @@ -28,14 +28,14 @@ suspend fun loadContributorsConcurrent(
}
```

With structured concurrency, you can specify the major context elements (like dispatcher) once, when creating the
top-level coroutine. All the nested coroutines then inherit the context and modify it only if needed.
With structured concurrency, you can specify major context elements (such as the dispatcher) once, when creating the
top-level coroutine. All nested coroutines then inherit this context and only modify it as needed.

<div class="hint">

> When you write code with coroutines for UI applications, for example Android ones, it's a common practice to
> use `CoroutineDispatchers.Main` by default for the top coroutine and then to explicitly put a different dispatcher when
> you need to run the code on a different thread.
> In the context of UI applications using coroutines, such as Android applications, a common practice involves
> using `CoroutineDispatchers.Main` as the default for the top coroutine. Then, to run code on a different thread, a different dispatcher is
> explicitly assigned.
</div>

For a more detailed description, you can look at [this article](https://kotlinlang.org/docs/coroutines-and-channels.html#using-the-outer-scope-s-context)
For a more detailed description, you can refer to [this article](https://kotlinlang.org/docs/coroutines-and-channels.html#using-the-outer-scope-s-context)