From 04eaa290522e87f3f0bfafa9f047d3d899e0e1bb Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 27 Jul 2022 20:41:04 +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 --- .../legado/app/help/coroutine/ActivelyCancelException.kt | 5 +++++ .../main/java/io/legado/app/help/coroutine/Coroutine.kt | 7 ++----- 2 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/coroutine/ActivelyCancelException.kt diff --git a/app/src/main/java/io/legado/app/help/coroutine/ActivelyCancelException.kt b/app/src/main/java/io/legado/app/help/coroutine/ActivelyCancelException.kt new file mode 100644 index 000000000..a18738f84 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/coroutine/ActivelyCancelException.kt @@ -0,0 +1,5 @@ +package io.legado.app.help.coroutine + +import kotlin.coroutines.cancellation.CancellationException + +class ActivelyCancelException : CancellationException() \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt index 36a21428c..6f4045f37 100644 --- a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt +++ b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt @@ -40,8 +40,6 @@ class Coroutine( private var timeMillis: Long? = null private var errorReturn: Result? = null - private var isCancelCalled = false - val isCancelled: Boolean get() = job.isCancelled @@ -116,8 +114,7 @@ class Coroutine( } //取消当前任务 - fun cancel(cause: CancellationException? = null) { - isCancelCalled = true + fun cancel(cause: ActivelyCancelException = ActivelyCancelException()) { job.cancel(cause) cancel?.let { MainScope().launch { @@ -149,7 +146,7 @@ class Coroutine( success?.let { dispatchCallback(this, value, it) } } catch (e: Throwable) { e.printOnDebug() - if (e is CancellationException && !isCancelCalled) this@Coroutine.cancel() + if (e is ActivelyCancelException) this@Coroutine.cancel() if (e is CancellationException && e !is TimeoutCancellationException) { return@launch }