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