For anyone working with `WorkManager` ’s `Coroutin...
# android
x
For anyone working with
WorkManager
’s `CoroutineWorker`s, is there a way to run clean up work during
onStopped()
? looks like this is marked as
final
in the implementation for some reason
e
coroutineworker cancels the job on stop, so
Copy code
override suspend fun doWork(): Result = try {
    ...
} catch (e: CancellationException) {
    // run on cancellation (note: cannot suspend here)
    throw e
}
should be executed when that happens
i
Same rules as clean up of any suspending method apply here as well
Personally, a
finally
block seems like the appropriate place for unconditional clean up work
e
I agree that
finally
is almost always what you want to handle instead of
CancellationException
, yes
c
TIL about the
finally
block
126 Views