[Android] Prevent Exception when coroutine is cancelled

Geoffrey Métais git at videolan.org
Thu Jan 31 11:42:26 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan 31 10:28:22 2019 +0100| [734c4b8108f4de8e4ca2b61701cecaf627c514e0] | committer: Geoffrey Métais

Prevent Exception when coroutine is cancelled

> https://code.videolan.org/videolan/vlc-android/commit/734c4b8108f4de8e4ca2b61701cecaf627c514e0
---

 vlc-android/src/org/videolan/vlc/util/Kextensions.kt | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index 3e2fcab28..5fe29d76f 100644
--- a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -74,8 +74,10 @@ suspend inline fun <reified T> Context.getFromMl(crossinline block: Medialibrary
     else suspendCancellableCoroutine { continuation ->
         val listener = object : Medialibrary.OnMedialibraryReadyListener {
             override fun onMedialibraryReady() {
-                continuation.resume(block.invoke(ml))
-                let { launch { ml.removeOnMedialibraryReadyListener(it) } }
+                if (!continuation.isCancelled) {
+                    continuation.resume(block.invoke(ml))
+                    let { launch { ml.removeOnMedialibraryReadyListener(it) } }
+                }
             }
             override fun onMedialibraryIdle() {}
         }



More information about the Android mailing list