https://kotlinlang.org logo
Title
b

brabo-hi

12/21/2021, 9:56 PM
Hi all, when using workmanager, how could we listen when worker is stopped within the worker ?
n

Nick Allen

12/21/2021, 11:51 PM
Assuming you are talking about CoroutineWorker. If the coroutine is cancelled, then a
CancellationException
will be thrown from suspending methods.
override suspend fun doWork(): Result {
    try {
        doStuffInSuspendMethod()
        return Result.success()
    } catch (ce: CancellationException) {
        handleCancellationSpecifically()
        throw ce
    }
}
I rarely need to do anything in response to cancellation specifically and usually just use a finally block. There's also
isActive
property on
CoroutineScope
and
CoroutineContext
.
b

brabo-hi

12/22/2021, 12:33 AM
thank you, i only see
isStopped