Hi Folks, I am trying to implement the Structured ...
# coroutines
n
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
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
thanks for your response... @octylFractal by failure I mean onResponse of service call like if status !=400
o
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
@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
basically, yes
n
or may be simply put them both in one parent and then cancel parent directly....
o
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
yup
oh I see cancelling any child will automatically cancel all other
thanks buddy
z
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
huh, you're right. mis-remembrance on my part
n
thanks @Zach Klippenstein (he/him) [MOD] I ll cancel parent as soon as I get into any of the failure scenarios of any child