From 73118d4af7f1d5ad4e7ea60d6bf183d778e6350a Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Sat, 20 May 2023 09:06:06 +0700 Subject: [PATCH] DownloadCache: Fix freezing on initial loading of cache file (#9523) --- .../tachiyomi/data/download/DownloadCache.kt | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt index 03caeb551c..f9391b3aaa 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt @@ -26,7 +26,7 @@ import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.flow.stateIn -import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withTimeoutOrNull @@ -96,7 +96,7 @@ class DownloadCache( get() = File(context.cacheDir, "dl_index_cache") private val rootDownloadsDirLock = Mutex() - private var rootDownloadsDir: RootDirectory + private var rootDownloadsDir = RootDirectory(getDirectoryFromPreference()) init { downloadPreferences.downloadsDirectory().changes() @@ -106,20 +106,20 @@ class DownloadCache( } .launchIn(scope) - rootDownloadsDir = runBlocking(Dispatchers.IO) { - try { - val diskCache = diskCacheFile.inputStream().use { - ProtoBuf.decodeFromByteArray(it.readBytes()) + // Attempt to read cache file + scope.launch { + rootDownloadsDirLock.withLock { + try { + val diskCache = diskCacheFile.inputStream().use { + ProtoBuf.decodeFromByteArray(it.readBytes()) + } + rootDownloadsDir = diskCache + lastRenew = System.currentTimeMillis() + } catch (e: Throwable) { + diskCacheFile.delete() } - lastRenew = 1 // Just so that the banner won't show up - diskCache - } catch (e: Throwable) { - diskCacheFile.delete() - null } - } ?: RootDirectory(getDirectoryFromPreference()) - - notifyChanges() + } } /**