From e2932b3833203e559218b9adc7451681d6e3c3e8 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 24 Apr 2022 10:47:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/utils/RegexExtensions.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/RegexExtensions.kt b/app/src/main/java/io/legado/app/utils/RegexExtensions.kt index 79357fbcc..61ba70e0a 100644 --- a/app/src/main/java/io/legado/app/utils/RegexExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/RegexExtensions.kt @@ -9,6 +9,8 @@ import kotlinx.coroutines.suspendCancellableCoroutine import splitties.init.appCtx import kotlin.coroutines.resume +private val scope = MainScope() + /** * 带有超时检测的正则替换 * 超时重启apk,线程不能强制结束,只能重启apk @@ -16,7 +18,7 @@ import kotlin.coroutines.resume suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String { val charSequence = this return suspendCancellableCoroutine { block -> - val scope = MainScope().launch(IO) { + val job = scope.launch(IO) { try { val result = regex.replace(charSequence, replacement) block.resume(result) @@ -25,7 +27,7 @@ suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Lon } } mainHandler.postDelayed({ - if (scope.isActive) { + if (job.isActive) { val timeoutMsg = "替换超时,将在3秒后重启应用\n替换规则$regex\n替换内容:${this}" val exception = RegexTimeoutException(timeoutMsg) block.cancel(exception)