https://kotlinlang.org logo
Title
n

Nav Singh

11/02/2019, 4:51 AM
Hi Folks, I am trying to implement the Structured concurrency using Coroutines.... I fire 2 network calls parallel they work fine....but I have a use Case that I need to cancel one of the them as soon as I get failure response for one of them . Any suggestions appreciated Thanks
o

octylFractal

11/02/2019, 4:52 AM
if they share a parent job, this automatically happens due to structured concurrency, unless you introduce SupervisorJob
also assuming that your coroutines listen for cancellation during the network call, as cancellation is cooperative
n

Nav Singh

11/02/2019, 4:54 AM
thanks for your response... @octylFractal by failure I mean onResponse of service call like if status !=400
o

octylFractal

11/02/2019, 4:56 AM
if you're not able to throw an exception / cancel the current coroutine, then you'll just have to have a reference to the Job/Deferred/etc. of the other call (returned from `launch`/`async`/`produce`...)
n

Nav Singh

11/02/2019, 4:58 AM
@octylFractal this sounds related I ll reference both these jobs in their failure case, and cancel other job as soon as one of them get failed
like Job1[faliure case] = job2.cancel
job2[faliure case] = job1.cancel
o

octylFractal

11/02/2019, 4:59 AM
basically, yes
n

Nav Singh

11/02/2019, 5:00 AM
or may be simply put them both in one parent and then cancel parent directly....
o

octylFractal

11/02/2019, 5:01 AM
in that case you could just cancel the current coroutine. they should likely share a parent Job already if you're implementing structured concurrency right
n

Nav Singh

11/02/2019, 5:11 AM
yup
oh I see cancelling any child will automatically cancel all other
thanks buddy
z

Zach Klippenstein (he/him) [MOD]

11/03/2019, 1:57 AM
Cancelling a child won't cancel its siblings. If a job fails, all siblings and the parent will be cancelled. Cancelling a parent will cancel all its children.
👍 2
o

octylFractal

11/03/2019, 2:10 AM
huh, you're right. mis-remembrance on my part
n

Nav Singh

11/04/2019, 12:35 AM
thanks @Zach Klippenstein (he/him) [MOD] I ll cancel parent as soon as I get into any of the failure scenarios of any child