Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to know if your coroutine has been canceled from within a suspend function

I'm using a CoroutineWorker but all my business logic is a separate class which I start using a suspend function. I would prefer to keep all the logic in this class but I need to know if the work request has been canceled. Is there someway to know in the suspend function if it's been canceled?

like image 275
ExNatura Avatar asked Sep 11 '25 08:09

ExNatura


1 Answers

Repeating gmk57's answer:

"Plain suspend function (without withContext) doesn't provide isActive flag, but you can use coroutineContext.isActive"

With a suspend function you do have access to your corountineContext. Accessing that will give you access to all of the same properties that you can access from directly inside the context.

like image 113
MinceMan Avatar answered Sep 12 '25 22:09

MinceMan