@@ -527,28 +527,37 @@ class MainActivity : ScopedAppActivity() {
527
527
Every coroutine launched from within a ` MainActivity ` has its job as a parent and is immediately cancelled when
528
528
activity is destroyed.
529
529
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)
532
534
533
535
``` kotlin
534
536
class ActivityWithPresenters : ScopedAppActivity () {
535
537
fun init () {
536
538
val presenter = Presenter ()
537
- val presenter2 = NonSuspendingPresenter (this )
539
+ val presenter2 = ScopedPresenter (this )
538
540
}
539
541
}
540
542
541
543
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
544
550
}
545
551
}
546
552
547
- class NonSuspendingPresenter (scope : CoroutineScope ): CoroutineScope by scope {
553
+ class ScopedPresenter (scope : CoroutineScope ): CoroutineScope by scope {
548
554
fun loadData () = launch { // Extension on ActivityWithPresenters's scope
549
- // Implementation
550
555
}
551
556
}
557
+
558
+ suspend fun CoroutineScope.launchInIO () = launch(Dispatchers .IO ) {
559
+ // Launched in the scope of the caller, but with IO dispatcher
560
+ }
552
561
```
553
562
554
563
Parent-child relation between jobs forms a hierarchy. A coroutine that performs some background job on behalf of
@@ -709,6 +718,7 @@ After delay
709
718
[ Job ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html
710
719
[ Job.cancel ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/cancel.html
711
720
[ 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
712
722
[ withContext ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/with-context.html
713
723
[ Dispatchers.Default ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html
714
724
[ CoroutineStart ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-start/index.html
0 commit comments