Alexandru Hadăr
05/20/2022, 1:28 PMcurrentCoroutineContext()
function?
I want to cancel my listener when my coroutine is no longer active. I don’t have access to the scope, but I can use currentCoroutineContext()
since i’m inside a suspend function. Could this have any negative impact? Like memory leak or a false-positive?
private suspend fun doEffects(colorChangeCallback: OnColorChangedCallback, valueAnimator: ValueAnimator) {
val context: CoroutineContext = currentCoroutineContext()
valueAnimator.addUpdateListener {
if (context.isActive) {
// Do your work
} else {
// Cancel other work
valueAnimator.cancel()
}
}
}
Dmitry Khalanskiy [JB]
05/20/2022, 1:32 PMsuspend fun main
), currentCoroutineContext
will be empty.Alexandru Hadăr
05/20/2022, 1:36 PMjulian
05/20/2022, 5:23 PMsuspendCancellableCoroutine
and invokeOnCancellation
?Alexandru Hadăr
05/20/2022, 5:24 PM