I am launching a coroutine like `scope.launch{ // ...
# coroutines
v
I am launching a coroutine like
scope.launch{ // doing some work here which is having loops }
I want to stop doing that work on button click so I tried doing like this:
job = scope.launch{ // doing some work here which is having loops }
and then canceling the job :
job?.cancel
but it seems like loops are executing till termination and the job is not getting canceled Any other workaround?
t
I guess you have to add
yield()
in the point of the loop you want to cancel the coroutine if the parent job is cancelled. I’m trying to find some link to support this, I’ll post here if I find it.
v
okay thanks trying out these
n
Be careful with non-suspending loops in coroutines. If your code never yields or suspends, then it’s blocking other coroutines from being able to use that thread. This kind of code can clog up the entire dispatcher if all its threads are busy running loops.