@@ -572,23 +572,21 @@ We also need a convenient way to retrieve a job for any view in the application.
572
572
an activity is an Android ` Context ` of the views in it, so we can define the following ` View.contextJob ` extension property:
573
573
574
574
``` kotlin
575
- val View .contextJob: Job
576
- get() = (context as ? JobHolder )?.job ? : NonCancellable
575
+ val View .contextJob: Job ?
576
+ get() = (context as ? JobHolder )?.job
577
577
```
578
578
579
- Here we use [ NonCancellable] implementation of the ` Job ` as a null-object for the case where our ` contextJob `
580
- extension property is invoked in a context that does not have an attached job.
579
+ A convenience of having a ` contextJob ` available is that we can simply use it as the parent of all the coroutines
580
+ we start without having to worry about explicitly maintaining a list of the coroutines we had
581
+ started. All the life-cycle management will be taken care of by the mechanics of parent-child relations between
582
+ jobs.
583
+
584
+ For example, the ` View.onClick ` extension from the previous section can now be defined using ` contextJob ` :
581
585
582
- A convenience of having a ` contextJob ` available is that we can simply use it to start all the coroutines
583
- without having to worry about explicitly maintaining a list of the coroutines we had started.
584
- All the life-cycle management will be taken care of by the mechanics of parent-child relations between jobs.
585
-
586
- For example, ` View.onClick ` extension from the previous section can now be defined using ` contextJob ` :
587
-
588
586
``` kotlin
589
587
fun View.onClick (action : suspend () -> Unit ) {
590
588
// launch one actor as a parent of the context job
591
- val eventActor = actor<Unit >(contextJob + UI , capacity = Channel .CONFLATED ) {
589
+ val eventActor = actor<Unit >(UI , parent = contextJob , capacity = Channel .CONFLATED ) {
592
590
for (event in channel) action()
593
591
}
594
592
// install a listener to activate this actor
@@ -598,14 +596,13 @@ fun View.onClick(action: suspend () -> Unit) {
598
596
}
599
597
```
600
598
601
- Notice how ` contextJob + UI ` expression is used to start an actor in the above code. It defines a coroutine context
602
- for our new actor that includes the job and the ` UI ` dispatcher. The coroutine that is started by this
603
- ` actor(contextJob + UI) ` expression is going to become a child of the job of the corresponding context. When the
604
- activity is destroyed and its job is cancelled all its children coroutines are cancelled, too.
599
+ Notice how we used ` parent = contextJob ` to start an actor in the above code. The coroutine that is started this
600
+ way is going to become a child of the job of the corresponding context. When the activity is destroyed and its job
601
+ is cancelled, all its children coroutines are cancelled, too.
605
602
606
603
Parent-child relation between jobs forms a hierarchy. A coroutine that performs some background job on behalf of
607
604
the view and in its context can create further children coroutines. The whole tree of coroutines gets cancelled
608
- when the parent job is cancelled. An example of that is shown in
605
+ when the parent job is cancelled. An example of that is shown in the
609
606
[ "Children of a coroutine"] ( ../coroutines-guide.md#children-of-a-coroutine ) section of the guide to coroutines.
610
607
611
608
### Starting coroutine in UI event handlers without dispatch
0 commit comments