Introduce runAssertingNotMainThread as workaround

This commit is contained in:
tarsin committed 2023-07-12 10:37:00 +08:00
1 parent fed5285a31
commit 36895de83b
4 files changed
+9 -7

No files matched your search

+1 -3
View File
@@ -91,9 +91,7 @@ object EhDB {
dao.deleteByKey(gid)
}
@get:Synchronized
val allDownloadLabelList: List<DownloadLabel>
get() = db.downloadLabelDao().list()
suspend fun getAllDownloadLabelList() = db.downloadLabelDao().list()
suspend fun addDownloadLabel(label: String): DownloadLabel {
val dao = db.downloadLabelDao()
@@ -9,7 +9,7 @@ import androidx.room.Update
@Dao
interface DownloadLabelDao {
@Query("SELECT * FROM DOWNLOAD_LABELS ORDER BY TIME ASC")
fun list(): List<DownloadLabel>
suspend fun list(): List<DownloadLabel>
@Query("SELECT * FROM DOWNLOAD_LABELS ORDER BY TIME ASC LIMIT :limit OFFSET :offset")
fun list(offset: Int, limit: Int): List<DownloadLabel>
@@ -35,6 +35,7 @@ import com.hippo.ehviewer.util.ConcurrentPool
import com.hippo.ehviewer.util.LongList
import com.hippo.ehviewer.util.SimpleHandler
import com.hippo.ehviewer.util.assertNotMainThread
import com.hippo.ehviewer.util.runAssertingNotMainThread
import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.withUIContext
@@ -47,7 +48,6 @@ import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import splitties.init.appCtx
import splitties.preferences.edit
import java.util.LinkedList
@@ -63,7 +63,7 @@ object DownloadManager : OnSpiderListener {
private val map: MutableMap<String?, LinkedList<DownloadInfo>>
// All labels without default label
val labelList = EhDB.allDownloadLabelList as MutableList<DownloadLabel>
val labelList = runAssertingNotMainThread { EhDB.getAllDownloadLabelList() } as MutableList<DownloadLabel>
// Store download info with default label
val defaultInfoList: LinkedList<DownloadInfo>
@@ -79,7 +79,7 @@ object DownloadManager : OnSpiderListener {
init {
assertNotMainThread()
map = runBlocking {
map = runAssertingNotMainThread {
allInfoList.groupBy { it.label }.entries.associate {
it.key?.let { label ->
if (!containLabel(label)) {
@@ -16,6 +16,8 @@
package com.hippo.ehviewer.util
import android.os.Looper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
import java.io.BufferedReader
import java.io.FileReader
import java.util.regex.Pattern
@@ -73,3 +75,5 @@ val isMainThread: Boolean
fun assertNotMainThread() {
check(!isMainThread) { "Cannot access database on the main thread since" + " it may potentially lock the UI for a long period of time." }
}
fun <T> runAssertingNotMainThread(block: suspend CoroutineScope.() -> T) = assertNotMainThread().run { runBlocking(block = block) }