Skip to content

Commit 29bfa1e

Browse files
Marko Topolnikqwwdfsad
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 1ce6c0b commit 29bfa1e

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

ui/coroutines-guide-ui.md

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

586586
```kotlin
587-
val View.contextJob: Job
588-
get() = (context as? JobHolder)?.job ?: NonCancellable
587+
val View.contextJob: Job?
588+
get() = (context as? JobHolder)?.job
589589
```
590590

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

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-
600598
```kotlin
601599
fun View.onClick(action: suspend () -> Unit) {
602600
// 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) {
604602
for (event in channel) action()
605603
}
606604
// install a listener to activate this actor
@@ -610,14 +608,13 @@ fun View.onClick(action: suspend () -> Unit) {
610608
}
611609
```
612610

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

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

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

0 commit comments

Comments
 (0)