Преглед изворни кода

fix: Fragment未初始化View执行scopeNetLife导致空指针

drake пре 2 година
родитељ
комит
1a219dba0b

+ 8 - 1
net/src/main/java/com/drake/net/time/Interval.kt

@@ -223,7 +223,14 @@ open class Interval @JvmOverloads constructor(
         fragment: Fragment,
         lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY
     ): Interval {
-        return life(fragment.viewLifecycleOwner, lifeEvent)
+        fragment.viewLifecycleOwnerLiveData.observe(fragment) {
+            it?.lifecycle?.addObserver(object : LifecycleEventObserver {
+                override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
+                    if (lifeEvent == event) cancel()
+                }
+            })
+        }
+        return this
     }
 
     /**

+ 23 - 2
net/src/main/java/com/drake/net/utils/Scope.kt

@@ -21,6 +21,7 @@ import android.view.View
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.FragmentActivity
 import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleEventObserver
 import androidx.lifecycle.LifecycleOwner
 import com.drake.brv.PageRefreshLayout
 import com.drake.net.scope.*
@@ -74,7 +75,17 @@ fun Fragment.scopeLife(
     lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
     dispatcher: CoroutineDispatcher = Dispatchers.Main,
     block: suspend CoroutineScope.() -> Unit
-) = AndroidScope(viewLifecycleOwner, lifeEvent, dispatcher).launch(block)
+): AndroidScope {
+    val coroutineScope = AndroidScope(dispatcher = dispatcher).launch(block)
+    viewLifecycleOwnerLiveData.observe(this) {
+        it?.lifecycle?.addObserver(object : LifecycleEventObserver {
+            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
+                if (lifeEvent == event) coroutineScope.cancel()
+            }
+        })
+    }
+    return coroutineScope
+}
 //</editor-fold>
 
 // <editor-fold desc="加载对话框">
@@ -211,7 +222,17 @@ fun Fragment.scopeNetLife(
     lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
     dispatcher: CoroutineDispatcher = Dispatchers.Main,
     block: suspend CoroutineScope.() -> Unit
-) = NetCoroutineScope(viewLifecycleOwner, lifeEvent, dispatcher).launch(block)
+): NetCoroutineScope {
+    val coroutineScope = NetCoroutineScope(dispatcher = dispatcher).launch(block)
+    viewLifecycleOwnerLiveData.observe(this) {
+        it?.lifecycle?.addObserver(object : LifecycleEventObserver {
+            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
+                if (lifeEvent == event) coroutineScope.cancel()
+            }
+        })
+    }
+    return coroutineScope
+}
 
 
 //</editor-fold>