You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Update CoroutineScope docs
* Fixed scope examples in guides, added notes on first-party support in Android.
* Simplified scopes section in UI guide since it is mostly irrelevant.
FixesKotlin#1581
Copy file name to clipboardExpand all lines: kotlinx-coroutines-core/common/src/CoroutineScope.kt
+25-13
Original file line number
Diff line number
Diff line change
@@ -10,37 +10,49 @@ import kotlin.coroutines.*
10
10
importkotlin.coroutines.intrinsics.*
11
11
12
12
/**
13
-
* Defines a scope for new coroutines. Every coroutine builder
13
+
* Defines a scope for new coroutines. Every **coroutine builder** (like [launch], [async], etc)
14
14
* 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.
16
16
*
17
17
* The best ways to obtain a standalone instance of the scope are [CoroutineScope()] and [MainScope()] factory functions.
18
18
* Additional context elements can be appended to the scope using the [plus][CoroutineScope.plus] operator.
19
19
*
20
+
* ### Convention for structured concurrency
21
+
*
20
22
* 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.
22
25
*
23
-
* Every coroutine builder (like [launch][CoroutineScope.launch], [async][CoroutineScope.async], etc)
26
+
* Every coroutine builder (like [launch], [async], etc)
24
27
* and every scoping function (like [coroutineScope], [withContext], etc) provides _its own_ scope
25
28
* with its own [Job] instance into the inner block of code it runs.
26
29
* 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.
28
31
*
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 the 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:
32
41
*
33
42
* ```
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 ...
37
49
* }
38
50
*
39
51
* /*
40
-
* * Note how coroutine builders are scoped: if activity is destroyed or any of the launched coroutines
52
+
* * Note: if this instance is destroyed or any of the launched coroutines
41
53
* * in this method throws an exception, then all nested coroutines are cancelled.
42
54
* */
43
-
* fun showSomeData() = launch { // <- extension on current activity, launched in the main thread
55
+
* fun showSomeData() = scope.launch { // launched in the main thread
44
56
* // ... here we can use suspending functions or coroutine builders with other dispatchers
0 commit comments