@@ -45,16 +45,14 @@ import kotlin.coroutines.*
45
45
* * Note how coroutine builders are scoped: if activity is destroyed or any of the launched coroutines
46
46
* * in this method throws an exception, then all nested coroutines are cancelled.
47
47
* */
48
- * fun loadDataFromUI() = launch { // <- extension on current activity, launched in the main thread
49
- * val ioData = async(Dispatchers.IO) { // <- extension on launch scope, launched in IO dispatcher
48
+ * fun showSomeData() = launch { // <- extension on current activity, launched in the main thread
49
+ * val data = withContext(Dispatchers.IO) {
50
+ * // Provides withContext scope that is child of he outer launch scope
50
51
* // blocking I/O operation
51
52
* }
52
- * // do something else concurrently with I/O
53
- * val data = ioData.await() // wait for result of I/O
54
- * draw(data) // can draw in the main thread
53
+ * draw(data) // draw in the main thread
55
54
* }
56
55
* }
57
- *
58
56
* ```
59
57
*/
60
58
public interface CoroutineScope {
@@ -157,10 +155,10 @@ public object GlobalScope : CoroutineScope {
157
155
* Example of the scope usages looks like this:
158
156
*
159
157
* ```
160
- * suspend fun loadDataForUI () = coroutineScope {
158
+ * suspend fun showSomeData () = coroutineScope {
161
159
*
162
- * val data = async { // <- extension on current scope
163
- * ... load some UI data ...
160
+ * val data = async(Dispatchers.IO) { // <- extension on current scope
161
+ * ... load some UI data for the Main thread ...
164
162
* }
165
163
*
166
164
* withContext(Dispatchers.Main) {
@@ -172,9 +170,10 @@ public object GlobalScope : CoroutineScope {
172
170
* ```
173
171
*
174
172
* Semantics of the scope in this example:
175
- * 1) `loadDataForUI` returns as soon as data is loaded and UI is updated.
176
- * 2) If `doSomeWork` throws an exception, then `async` task is cancelled and `loadDataForUI` rethrows that exception.
177
- * 3) If outer scope of `loadDataForUI` is cancelled, both started `async` and `withContext` are cancelled.
173
+ * 1) `showSomeData` returns as soon as data is loaded and displayed in the UI.
174
+ * 2) If `doSomeWork` throws an exception, then `async` task is cancelled and `showSomeData` rethrows that exception.
175
+ * 3) If outer scope of `showSomeData` is cancelled, both started `async` and `withContext` blocks are cancelled.
176
+ * 4) If `async` block fails, `withContext` will be cancelled.
178
177
*
179
178
* Method may throw [CancellationException] if the current job was cancelled externally
180
179
* or may throw the corresponding unhandled [Throwable] if there is any unhandled exception in this scope
0 commit comments