diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index c2b36a80e..1683d9fee 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -11,6 +11,10 @@ * 正文出现缺字漏字、内容缺失、排版错乱等情况,有可能是净化规则或简繁转换出现问题。 * 漫画源看书显示乱码,**阅读与其他软件的源并不通用**,请导入阅读的支持的漫画源! +**2022/06/14** + +* 导出使用单线程,导出占用大量内存,并行容易OOM + **2022/06/10** * 更新cronet: 102.0.5005.125 diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index d08e0c956..7b66ff6c7 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -24,7 +24,10 @@ import io.legado.app.help.ContentProcessor import io.legado.app.help.config.AppConfig import io.legado.app.utils.* import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.delay import kotlinx.coroutines.ensureActive +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import me.ag2s.epublib.domain.* import me.ag2s.epublib.epub.EpubWriter import me.ag2s.epublib.util.ResourceUtil @@ -40,6 +43,10 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { val upAdapterLiveData = MutableLiveData() val exportProgress = ConcurrentHashMap() val exportMsg = ConcurrentHashMap() + private val mutex = Mutex() + + @Volatile + private var exportNumber = 0 private fun getExportFileName(book: Book): String { val jsStr = AppConfig.bookExportFileName @@ -62,6 +69,12 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { exportMsg.remove(book.bookUrl) upAdapterLiveData.postValue(book.bookUrl) execute { + mutex.withLock { + while (exportNumber > 0) { + delay(1000) + } + exportNumber++ + } if (path.isContentScheme()) { val uri = Uri.parse(path) val doc = DocumentFile.fromTreeUri(context, uri) @@ -79,6 +92,8 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { exportProgress.remove(book.bookUrl) exportMsg[book.bookUrl] = context.getString(R.string.export_success) upAdapterLiveData.postValue(book.bookUrl) + }.onFinally { + exportNumber-- } } @@ -202,6 +217,12 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { exportMsg.remove(book.bookUrl) upAdapterLiveData.postValue(book.bookUrl) execute { + mutex.withLock { + while (exportNumber > 0) { + delay(1000) + } + exportNumber++ + } if (path.isContentScheme()) { val uri = Uri.parse(path) val doc = DocumentFile.fromTreeUri(context, uri) @@ -219,6 +240,8 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { exportProgress.remove(book.bookUrl) exportMsg[book.bookUrl] = context.getString(R.string.export_success) upAdapterLiveData.postValue(book.bookUrl) + }.onFinally { + exportNumber-- } }