From 6aee4fc464d957ba30a86fc3d13fad397c6abc95 Mon Sep 17 00:00:00 2001 From: jmir1 Date: Fri, 17 Jun 2022 05:34:44 +0200 Subject: [PATCH] Fix freezing on migrating manga (#7317) * Use `supend` instead of `runBlocking` in migrate function * lift `syncChaptersWithSource` out of the db trasaction --- .../ui/browse/migration/search/SearchPresenter.kt | 14 +++++++------- .../tachiyomi/util/chapter/ChapterSourceSync.kt | 7 ++----- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt index 36e54f580d..9b6b873303 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt @@ -85,7 +85,7 @@ class SearchPresenter( } } - private fun migrateMangaInternal( + private suspend fun migrateMangaInternal( prevSource: Source?, source: Source, sourceChapters: List, @@ -111,15 +111,15 @@ class SearchPresenter( flags, ) + try { + syncChaptersWithSource(sourceChapters, manga, source) + } catch (e: Exception) { + // Worst case, chapters won't be synced + } + db.inTransaction { // Update chapters read if (migrateChapters) { - try { - syncChaptersWithSource(sourceChapters, manga, source) - } catch (e: Exception) { - // Worst case, chapters won't be synced - } - val prevMangaChapters = db.getChapters(prevManga).executeAsBlocking() val maxChapterRead = prevMangaChapters .filter { it.read } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSourceSync.kt b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSourceSync.kt index cec3615c68..43ecf2b3a0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSourceSync.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSourceSync.kt @@ -5,7 +5,6 @@ import eu.kanade.domain.chapter.model.toDbChapter import eu.kanade.tachiyomi.data.database.models.toDomainManga import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.model.SChapter -import kotlinx.coroutines.runBlocking import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import eu.kanade.tachiyomi.data.database.models.Chapter as DbChapter @@ -19,16 +18,14 @@ import eu.kanade.tachiyomi.data.database.models.Manga as DbManga * @param source the source of the chapters. * @return a pair of new insertions and deletions. */ -fun syncChaptersWithSource( +suspend fun syncChaptersWithSource( rawSourceChapters: List, manga: DbManga, source: Source, syncChaptersWithSource: SyncChaptersWithSource = Injekt.get(), ): Pair, List> { val domainManga = manga.toDomainManga() ?: return Pair(emptyList(), emptyList()) - val (added, deleted) = runBlocking { - syncChaptersWithSource.await(rawSourceChapters, domainManga, source) - } + val (added, deleted) = syncChaptersWithSource.await(rawSourceChapters, domainManga, source) val addedDbChapters = added.map { it.toDbChapter() } val deletedDbChapters = deleted.map { it.toDbChapter() }