瀏覽代碼

处理并发择优参数为空情况

drake 4 年之前
父節點
當前提交
57a00ccc7b
共有 2 個文件被更改,包括 9 次插入3 次删除
  1. 1 1
      README.md
  2. 8 2
      net/src/main/java/com/drake/net/utils/Fastest.kt

+ 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.13'
 
-implementation 'com.github.liangjingkanji:Net:2.2.20'
+implementation 'com.github.liangjingkanji:Net:2.2.21'
 ```
 
 <br>

+ 8 - 2
net/src/main/java/com/drake/net/utils/Fastest.kt

@@ -38,6 +38,9 @@ suspend fun <T> CoroutineScope.fastest(
     uid: Any? = null
 ): T {
     val deferred = CompletableDeferred<T>()
+    if (listDeferred.isNullOrEmpty()) {
+        deferred.completeExceptionally(IllegalArgumentException("Fastest is null or empty"))
+    }
     val mutex = Mutex()
     listDeferred.forEach {
         launch(Dispatchers.IO) {
@@ -73,12 +76,15 @@ suspend fun <T> CoroutineScope.fastest(
 @OptIn(ExperimentalCoroutinesApi::class)
 @Suppress("SuspendFunctionOnCoroutineScope")
 suspend fun <T, R> CoroutineScope.fastest(
-    listDeferred: List<DeferredTransform<T, R>>,
+    listDeferred: List<DeferredTransform<T, R>>?,
     uid: Any? = null
 ): R {
     val deferred = CompletableDeferred<R>()
+    if (listDeferred.isNullOrEmpty()) {
+        deferred.completeExceptionally(IllegalArgumentException("Fastest is null or empty"))
+    }
     val mutex = Mutex()
-    listDeferred.forEach {
+    listDeferred?.forEach {
         launch(Dispatchers.IO) {
             try {
                 val result = it.deferred.await()