Просмотр исходного кода

修复catch发生异常导致的死循环

drake 4 лет назад
Родитель
Сommit
5a38d4d84f

+ 1 - 1
README.md

@@ -107,7 +107,7 @@ implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'
 // 支持自动下拉刷新和缺省页的(可选)
 implementation 'com.github.liangjingkanji:BRV:1.3.9'
 
-implementation 'com.github.liangjingkanji:Net:2.2.15'
+implementation 'com.github.liangjingkanji:Net:2.2.16'
 ```
 
 <br>

+ 14 - 16
net/src/main/java/com/drake/net/scope/AndroidScope.kt

@@ -27,9 +27,11 @@ import kotlin.coroutines.EmptyCoroutineContext
  * 异步协程作用域
  */
 @Suppress("unused", "MemberVisibilityCanBePrivate", "NAME_SHADOWING")
-open class AndroidScope(lifecycleOwner: LifecycleOwner? = null,
-                        lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
-                        val dispatcher: CoroutineDispatcher = Dispatchers.Main) : CoroutineScope {
+open class AndroidScope(
+    lifecycleOwner: LifecycleOwner? = null,
+    lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
+    val dispatcher: CoroutineDispatcher = Dispatchers.Main
+                       ) : CoroutineScope {
 
     init {
         lifecycleOwner?.lifecycle?.addObserver(object : LifecycleEventObserver {
@@ -47,7 +49,8 @@ open class AndroidScope(lifecycleOwner: LifecycleOwner? = null,
 
     val uid = exceptionHandler
 
-    override val coroutineContext: CoroutineContext = dispatcher + exceptionHandler + SupervisorJob()
+    override val coroutineContext: CoroutineContext =
+        dispatcher + exceptionHandler + SupervisorJob()
 
 
     open fun launch(block: suspend CoroutineScope.() -> Unit): AndroidScope {
@@ -60,23 +63,16 @@ open class AndroidScope(lifecycleOwner: LifecycleOwner? = null,
     }
 
     protected open fun catch(e: Throwable) {
-        launch(adjustDispatcher()) {
-            catch?.invoke(this@AndroidScope, e) ?: handleError(e)
-        }
+        catch?.invoke(this@AndroidScope, e) ?: handleError(e)
     }
 
     /**
      * @param e 如果发生异常导致作用域执行完毕, 则该参数为该异常对象, 正常结束则为null
      */
     protected open fun finally(e: Throwable?) {
-        launch(adjustDispatcher()) {
-            finally?.invoke(this@AndroidScope, e)
-        }
+        finally?.invoke(this@AndroidScope, e)
     }
 
-    protected fun adjustDispatcher() = if (dispatcher === Dispatchers.Main) dispatcher.immediate else dispatcher
-
-
     /**
      * 当作用域内发生异常时回调
      */
@@ -103,12 +99,14 @@ open class AndroidScope(lifecycleOwner: LifecycleOwner? = null,
 
     open fun cancel(cause: CancellationException? = null) {
         val job = coroutineContext[Job]
-                  ?: error("Scope cannot be cancelled because it does not have a job: $this")
+            ?: error("Scope cannot be cancelled because it does not have a job: $this")
         job.cancel(cause)
     }
 
-    open fun cancel(message: String,
-                    cause: Throwable? = null) = cancel(CancellationException(message, cause))
+    open fun cancel(
+        message: String,
+        cause: Throwable? = null
+                   ) = cancel(CancellationException(message, cause))
 
 }
 

+ 1 - 3
net/src/main/java/com/drake/net/scope/NetCoroutineScope.kt

@@ -86,9 +86,7 @@ open class NetCoroutineScope(lifecycleOwner: LifecycleOwner? = null,
     }
 
     override fun catch(e: Throwable) {
-        launch(adjustDispatcher()) {
-            catch?.invoke(this@NetCoroutineScope, e) ?: if (error) handleError(e)
-        }
+        catch?.invoke(this@NetCoroutineScope, e) ?: if (error) handleError(e)
     }
 
     /**