Skip to content

Commit e44c9b2

Browse files
committed
Update coroutines-ui-guide, provide alternatives to currentScope builder
Fixes #710
1 parent cd37d8e commit e44c9b2

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

ui/coroutines-guide-ui.md

+17-7
Original file line numberDiff line numberDiff line change
@@ -527,28 +527,37 @@ class MainActivity : ScopedAppActivity() {
527527
Every coroutine launched from within a `MainActivity` has its job as a parent and is immediately cancelled when
528528
activity is destroyed.
529529

530-
To propagate activity scope to its views and presenters, one can use [currentScope] builder which captures current
531-
scope. Another solution is to implement [CoroutineScope] in child elements using delegation, for example:
530+
To propagate activity scope to its views and presenters, multiple techniques can be used:
531+
- [coroutineScope] builder to provide a nested scope
532+
- Receive [CoroutineScope] in presenter method parameters
533+
- Make method extension on [CoroutineScope] (applicable only for top-level methods)
532534

533535
```kotlin
534536
class ActivityWithPresenters: ScopedAppActivity() {
535537
fun init() {
536538
val presenter = Presenter()
537-
val presenter2 = NonSuspendingPresenter(this)
539+
val presenter2 = ScopedPresenter(this)
538540
}
539541
}
540542

541543
class Presenter {
542-
suspend fun loadData() = currentScope {
543-
// Now we're in the scope of ActivityWithPresenters
544+
suspend fun loadData() = coroutineScope {
545+
// Nested scope of outer activity
546+
}
547+
548+
suspend fun loadData(uiScope: CoroutineScope) = uiScope.launch {
549+
// Invoked in the uiScope
544550
}
545551
}
546552

547-
class NonSuspendingPresenter(scope: CoroutineScope): CoroutineScope by scope {
553+
class ScopedPresenter(scope: CoroutineScope): CoroutineScope by scope {
548554
fun loadData() = launch { // Extension on ActivityWithPresenters's scope
549-
// Implementation
550555
}
551556
}
557+
558+
suspend fun CoroutineScope.launchInIO() = launch(Dispatchers.IO) {
559+
// Launched in the scope of the caller, but with IO dispatcher
560+
}
552561
```
553562

554563
Parent-child relation between jobs forms a hierarchy. A coroutine that performs some background job on behalf of
@@ -709,6 +718,7 @@ After delay
709718
[Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html
710719
[Job.cancel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/cancel.html
711720
[CoroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html
721+
[coroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html
712722
[withContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/with-context.html
713723
[Dispatchers.Default]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html
714724
[CoroutineStart]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-start/index.html

0 commit comments

Comments
 (0)