[Android] Browser: cancel task using a coroutineScope
Geoffrey Métais
git at videolan.org
Thu Aug 8 13:53:40 CEST 2019
vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Aug 2 15:12:48 2019 +0200| [b8cce1475834e431dd881895301ec1883dfad00b] | committer: Geoffrey Métais
Browser: cancel task using a coroutineScope
(cherry picked from commit 9778750a33f51727dcecaaa919d7f1451595b176)
> https://code.videolan.org/videolan/vlc-android/commit/b8cce1475834e431dd881895301ec1883dfad00b
---
vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index 2bdb0912c..b4080468b 100644
--- a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -53,7 +53,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
val loading = MutableLiveData<Boolean>().apply { value = false }
protected var mediabrowser: MediaBrowser? = null
- private var parsingJob : CompletableJob? = null
+ private var parsingJob : Job? = null
private val foldersContentMap = SimpleArrayMap<MediaLibraryItem, MutableList<MediaLibraryItem>>()
protected lateinit var browserChannel : Channel<Media>
@@ -171,9 +171,8 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
val directories: MutableList<AbstractMediaWrapper> = ArrayList()
val files: MutableList<AbstractMediaWrapper> = ArrayList()
foldersContentMap.clear()
- val job = SupervisorJob(coroutineContext[Job]).also { parsingJob = it }
- try {
- withContext(Dispatchers.IO+job) {
+ coroutineScope { // allow child coroutine to be cancelled without closing the actor.
+ parsingJob = launch (Dispatchers.IO) {
initBrowser()
var currentParsedPosition = -1
loop@ while (++currentParsedPosition < currentMediaList.size) {
@@ -218,7 +217,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
files.clear()
}
}
- } catch (ignored: CancellationException) {}
+ }
parsingJob = null
}
More information about the Android
mailing list