Skip to content

Commit 66a5a7a

Browse files
committed
Update CoroutineScope docs
Fixes #1581
1 parent 3fdd3fe commit 66a5a7a

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

kotlinx-coroutines-core/common/src/CoroutineScope.kt

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,49 @@ import kotlin.coroutines.*
1010
import kotlin.coroutines.intrinsics.*
1111

1212
/**
13-
* Defines a scope for new coroutines. Every coroutine builder
13+
* Defines a scope for new coroutines. Every **coroutine builder** (like [launch], [async], etc)
1414
* is an extension on [CoroutineScope] and inherits its [coroutineContext][CoroutineScope.coroutineContext]
15-
* to automatically propagate both context elements and cancellation.
15+
* to automatically propagate all its elements and cancellation.
1616
*
1717
* The best ways to obtain a standalone instance of the scope are [CoroutineScope()] and [MainScope()] factory functions.
1818
* Additional context elements can be appended to the scope using the [plus][CoroutineScope.plus] operator.
1919
*
20+
* ### Convention for structured concurrency
21+
*
2022
* Manual implementation of this interface is not recommended, implementation by delegation should be preferred instead.
21-
* By convention, the [context of a scope][CoroutineScope.coroutineContext] should contain an instance of a [job][Job] to enforce structured concurrency.
23+
* By convention, the [context of a scope][CoroutineScope.coroutineContext] should contain an instance of a
24+
* [job][Job] to enforce the discipline of **structured concurrency** with propagation of cancellation.
2225
*
23-
* Every coroutine builder (like [launch][CoroutineScope.launch], [async][CoroutineScope.async], etc)
26+
* Every coroutine builder (like [launch], [async], etc)
2427
* and every scoping function (like [coroutineScope], [withContext], etc) provides _its own_ scope
2528
* with its own [Job] instance into the inner block of code it runs.
2629
* By convention, they all wait for all the coroutines inside their block to complete before completing themselves,
27-
* thus enforcing the discipline of **structured concurrency**.
30+
* thus enforcing the structured concurrency. See [Job] documentation for more details.
2831
*
29-
* [CoroutineScope] should be implemented (or used as a field) on entities with a well-defined lifecycle that are responsible
30-
* for launching children coroutines. Example of such entity on Android is Activity.
31-
* Usage of this interface may look like this:
32+
* ### Android usage
33+
*
34+
* Android has first-party support for coroutine scope in all entities with lifecycle.
35+
* See [the corresponding documentation](https://developer.android.com/topic/libraries/architecture/coroutines#lifecyclescope).
36+
*
37+
* ### Custom usage
38+
*
39+
* [CoroutineScope] should be implemented or declared as a property on entities with a well-defined lifecycle that are
40+
* responsible for launching children coroutines, for example:
3241
*
3342
* ```
34-
* class MyActivity : AppCompatActivity(), CoroutineScope by MainScope() {
35-
* override fun onDestroy() {
36-
* cancel() // cancel is extension on CoroutineScope
43+
* class MyUIClass {
44+
* val scope = MainScope() // the scope of MyUIClass
45+
*
46+
* fun destroy() { // destroys an instance of MyUIClass
47+
* scope.cancel() // cancels all coroutines launched in this scope
48+
* // ... do the rest of cleanup here ...
3749
* }
3850
*
3951
* /*
40-
* * Note how coroutine builders are scoped: if activity is destroyed or any of the launched coroutines
41-
* * in this method throws an exception, then all nested coroutines are cancelled.
52+
* * Note: if this instance is destroyed or any of the launched coroutines
53+
* * in this method throw an exception, then all nested coroutines are cancelled.
4254
* */
43-
* fun showSomeData() = launch { // <- extension on current activity, launched in the main thread
55+
* fun showSomeData() = scope.launch { // launched in the main thread
4456
* // ... here we can use suspending functions or coroutine builders with other dispatchers
4557
* draw(data) // draw in the main thread
4658
* }

0 commit comments

Comments
 (0)