bod
04/04/2020, 3:17 PMsuspendCancellableCoroutine
I sometimes see code that calls cont.resume(result)
but when I try that, it's looking for a onCancellation
argument, I so I need to do cont.resume(result) {}
instead. Ideas why? (May be relevant: I'm in Kotlin-js).Dominaezzz
04/04/2020, 4:14 PMresume
needs an import I think. (It's an extension function).bod
04/04/2020, 4:23 PMimport kotlin.coroutines.resume
😄 Thanks!Orhan Tozan
04/04/2020, 10:25 PMsuspendCoroutine
instead of suspendCancellableCoroutine
?Dominaezzz
04/04/2020, 10:53 PMcont.invokeOnCancellation
is being used.bod
04/04/2020, 11:02 PMsuspendCancellableCoroutine
when I could use suspendCoroutine
🙂 Thanks a lotlouiscad
04/05/2020, 10:09 PMsuspendCoroutine
is when you cannot and don't want to support cancellation at all, which can lead to memory leaks in some cases if your code never resumes or resumes after a long time while it should have been cancelled.louiscad
04/05/2020, 10:10 PMsuspendCoroutine
when suspendCancellableCoroutine
could/should be used.Orhan Tozan
04/06/2020, 1:35 PMremoveEventListener
to the onCancellation?louiscad
04/06/2020, 1:52 PMonCancellation
or in a `try`/`finally` or `try`/`catch(e: CancellationException)` block surrounding the suspendCancellableCoroutine
call.
Here's a real world example that allows to wait for a click on a View
in an Android app: https://github.com/LouisCAD/Splitties/blob/49e2ee566730aaeb14b4fa9e395a677c3f214dba/modules/views-coroutines/src/androidMain/kotlin/splitties/views/coroutines/VisibilityAndClicks.kt#L34-L51