Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

298
Views
Coroutines the proper way to add a job as child of another?

Given we have job1 : Job and job2 : Job and we want to make job2 a child of job1 (they where created separately have no relation).

What is the correct way to declare that relationship? so that when job1 is cancelled job2 is cancelled as well...

I tried job1.attachChild(e1.job2 as ChildJob) but this is internal api. I do not want to do some hack when i launch job2 from job1 coroutine.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use a Job(parent: Job?) factory function which receives parent job as a parameter. It has the following definition:

public fun Job(parent: Job? = null): Job

that means parameter parent is an optional. So you can create your jobs like this:

var parentJob: Job = Job()
var childJob: Job = Job(parentJob)

Also take a look at SupervisorJob, which can be used to customize the default behavior of the Job. SupervisorJob factory function has similar definition:

fun SupervisorJob(parent: Job? = null): Job
over 4 years ago · Santiago Trujillo Report

0

Simply launch job2 from inside the scope of job1. Job2 will inherit the scope, and so if job1 is cancelled then so is job2.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!