|
@@ -22,9 +22,6 @@ import kotlinx.coroutines.CoroutineDispatcher
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
|
-
|
|
|
-private const val JOB_KEY = "com.drake.net.view-model"
|
|
|
-
|
|
|
/**
|
|
|
* 在[ViewModel]被销毁时取消协程作用域
|
|
|
*/
|
|
@@ -32,9 +29,8 @@ fun ViewModel.scopeLife(
|
|
|
dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
block: suspend CoroutineScope.() -> Unit
|
|
|
): AndroidScope {
|
|
|
- val scope: AndroidScope? = this.getTag(JOB_KEY)
|
|
|
- if (scope != null) return scope
|
|
|
- return setTagIfAbsent(JOB_KEY, AndroidScope(dispatcher = dispatcher).launch(block))
|
|
|
+ val scope = AndroidScope(dispatcher = dispatcher).launch(block)
|
|
|
+ return setTagIfAbsent(scope.toString(), scope)
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -45,7 +41,6 @@ fun ViewModel.scopeNetLife(
|
|
|
dispatcher: CoroutineDispatcher = Dispatchers.Main,
|
|
|
block: suspend CoroutineScope.() -> Unit
|
|
|
): NetCoroutineScope {
|
|
|
- val scope: NetCoroutineScope? = this.getTag(JOB_KEY)
|
|
|
- if (scope != null) return scope
|
|
|
- return setTagIfAbsent(JOB_KEY, NetCoroutineScope(dispatcher = dispatcher).launch(block))
|
|
|
+ val scope = NetCoroutineScope(dispatcher = dispatcher).launch(block)
|
|
|
+ return setTagIfAbsent(scope.toString(), scope)
|
|
|
}
|