From e4dc35674d680b10df2f059a5a51b01f892f9861 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 24 Jan 2021 10:33:29 -0500 Subject: [PATCH] Perform metadata update in global scope --- .../data/library/LibraryUpdateService.kt | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index 43f7d39edb..eb6ff78587 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -34,11 +34,14 @@ import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.util.system.acquireWakeLock import eu.kanade.tachiyomi.util.system.isServiceRunning import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job -import kotlinx.coroutines.MainScope +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.cancel +import kotlinx.coroutines.launch import kotlinx.coroutines.supervisorScope import timber.log.Timber import uy.kohesive.injekt.Injekt @@ -65,7 +68,7 @@ class LibraryUpdateService( private lateinit var wakeLock: PowerManager.WakeLock private lateinit var notifier: LibraryUpdateNotifier - private lateinit var scope: CoroutineScope + private lateinit var ioScope: CoroutineScope private var updateJob: Job? = null @@ -142,7 +145,7 @@ class LibraryUpdateService( override fun onCreate() { super.onCreate() - scope = MainScope() + ioScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) notifier = LibraryUpdateNotifier(this) wakeLock = acquireWakeLock(javaClass.name) @@ -154,7 +157,7 @@ class LibraryUpdateService( * lock. */ override fun onDestroy() { - scope?.cancel() + ioScope?.cancel() updateJob?.cancel() if (wakeLock.isHeld) { wakeLock.release() @@ -190,7 +193,7 @@ class LibraryUpdateService( val mangaList = getMangaToUpdate(intent, target) .sortedWith(rankingScheme[selectedScheme]) - updateJob = scope.launchIO { + updateJob = ioScope.launch { try { when (target) { Target.CHAPTERS -> updateChapterList(mangaList) @@ -329,17 +332,19 @@ class LibraryUpdateService( // Update manga details metadata in the background if (preferences.autoUpdateMetadata()) { - val updatedManga = source.getMangaDetails(manga.toMangaInfo()) - val sManga = updatedManga.toSManga() - // Avoid "losing" existing cover - if (!sManga.thumbnail_url.isNullOrEmpty()) { - manga.prepUpdateCover(coverCache, sManga, false) - } else { - sManga.thumbnail_url = manga.thumbnail_url - } + GlobalScope.launchIO { + val updatedManga = source.getMangaDetails(manga.toMangaInfo()) + val sManga = updatedManga.toSManga() + // Avoid "losing" existing cover + if (!sManga.thumbnail_url.isNullOrEmpty()) { + manga.prepUpdateCover(coverCache, sManga, false) + } else { + sManga.thumbnail_url = manga.thumbnail_url + } - manga.copyFrom(sManga) - db.insertManga(manga).executeAsBlocking() + manga.copyFrom(sManga) + db.insertManga(manga).executeAsBlocking() + } } val chapters = source.getChapterList(manga.toMangaInfo())