Skip to content

Commit b37c296

Browse files
committed
Replace UI with Dispatchers.Main in CoroutineScope documentation
Fixes #803
1 parent 4451d72 commit b37c296

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

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

+11-12
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,14 @@ import kotlin.coroutines.*
4545
* * Note how coroutine builders are scoped: if activity is destroyed or any of the launched coroutines
4646
* * in this method throws an exception, then all nested coroutines are cancelled.
4747
* */
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
5051
* // blocking I/O operation
5152
* }
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
5554
* }
5655
* }
57-
*
5856
* ```
5957
*/
6058
public interface CoroutineScope {
@@ -157,10 +155,10 @@ public object GlobalScope : CoroutineScope {
157155
* Example of the scope usages looks like this:
158156
*
159157
* ```
160-
* suspend fun loadDataForUI() = coroutineScope {
158+
* suspend fun showSomeData() = coroutineScope {
161159
*
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 ...
164162
* }
165163
*
166164
* withContext(Dispatchers.Main) {
@@ -172,9 +170,10 @@ public object GlobalScope : CoroutineScope {
172170
* ```
173171
*
174172
* 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.
178177
*
179178
* Method may throw [CancellationException] if the current job was cancelled externally
180179
* or may throw the corresponding unhandled [Throwable] if there is any unhandled exception in this scope

0 commit comments

Comments
 (0)