question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API for creating a parent job backed by a coroutineContext

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
DrewCarlsoncommented, Sep 7, 2018
val job = Job().apply { this + dispatcher }

The plus operator for CoroutineContext returns a new CombinedContext, it does not modify the Job.

I think this is what you are looking for:

val dispatcher = newSingleThreadContext("MyDispatcher")
val context = Job() + dispatcher
        
launch(context) {
  log(1)
}

Job.kt contains two extension functions for CoroutineContext that allow for cancelling the Job in this new context.

0reactions
ZakTaccardicommented, Sep 7, 2018

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.

val dispatcher = newSingleThreadContext("MyDispatcher")
val job = Job().apply { this + dispatcher }
        
launch(parent = job) {
  log(1)
}

I thought the coroutine would be dispatched by the dispatcher above, but it’s actually the DefaultDispatcher.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found