API for creating a parent job backed by a coroutineContext
See original GitHub issueI want to create a job parent for cancellation purposes. I want the parent job to run in a specific context. Currently, we have to do it this way:
private val jobParent = Job().apply { this + CommonPool }
I think passing that information into the constructor would make for a better and more obvious api.
private val jobParent = Job(CommonPool)
Is something like this possible?
Rest of usage:
launch(parentJob) { ... }
override fun onCleared() {
jobParent.cancelChildren()
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Coroutine context and dispatchers - Kotlin
The coroutine context includes a coroutine dispatcher (see CoroutineDispatcher) that determines what thread or threads the corresponding ...
Read more >What is the difference between CoroutineContext and Job in ...
The parent parameter to coroutine builders like launch is just a convenience to make parent job specification more explicit.
Read more >Kotlin Coroutine Job Hierarchy — Succeed, Fail, and Cancel
The documentation of this property explains how jobs are created with a relationship to their parent. A job becomes a child of this...
Read more >Coroutines: Context, Cancellation, SupervisorJob scenarios ...
To create an instance of CoroutineContext you can provide it set of ... parent scope will get all elements except Job from parent...
Read more >Job and children awaiting in Kotlin Coroutines - Kt. Academy
Coroutine builders create their jobs based on their parent job ... Since Job is a coroutine context, we can access it using coroutineContext[Job]...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The plus operator for
CoroutineContext
returns a newCombinedContext
, it does not modify theJob
.I think this is what you are looking for:
Job.kt
contains two extension functions forCoroutineContext
that allow for cancelling theJob
in this new context.the use case for this is creating a parent job for children to automatically have a dispatcher applied. I was under the impression this could be done, but I don’t think it can.
I thought the coroutine would be dispatched by the
dispatcher
above, but it’s actually theDefaultDispatcher
.