Skip to content

Commit 2a63775

Browse files
Marko Topolnikelizarov
Marko Topolnik
authored andcommitted
Use parent = contextJob instead of contextJob + UI
Since parent is `Job?`, change the type of `contextJob` to `Job?` as well and remove `NonCancellable` as the null-object.
1 parent 3975391 commit 2a63775

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

ui/coroutines-guide-ui.md

+13-16
Original file line numberDiff line numberDiff line change
@@ -572,23 +572,21 @@ We also need a convenient way to retrieve a job for any view in the application.
572572
an activity is an Android `Context` of the views in it, so we can define the following `View.contextJob` extension property:
573573

574574
```kotlin
575-
val View.contextJob: Job
576-
get() = (context as? JobHolder)?.job ?: NonCancellable
575+
val View.contextJob: Job?
576+
get() = (context as? JobHolder)?.job
577577
```
578578

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`:
581585

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-
588586
```kotlin
589587
fun View.onClick(action: suspend () -> Unit) {
590588
// 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) {
592590
for (event in channel) action()
593591
}
594592
// install a listener to activate this actor
@@ -598,14 +596,13 @@ fun View.onClick(action: suspend () -> Unit) {
598596
}
599597
```
600598

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.
605602

606603
Parent-child relation between jobs forms a hierarchy. A coroutine that performs some background job on behalf of
607604
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
609606
["Children of a coroutine"](../coroutines-guide.md#children-of-a-coroutine) section of the guide to coroutines.
610607

611608
### Starting coroutine in UI event handlers without dispatch

0 commit comments

Comments
 (0)