通过标签 Kotlin 从内部嵌套协程返回

Bra*_*Xia 3 asynchronous coroutine kotlin kotlin-coroutines coroutinescope

用例:我有很多操作想要从主线程异步发生,但又彼此并行。

val scope = CoroutineScope(Dispatchers.IO)
val items = // List of items to do something.

scope.launch {
    items.forEach { item ->
        scope.launch {
            if (itemFailsValidation(item)) {
                // Here I want to skip this item but continue the forEach loop.
                return@launch // "There is more than one label with such a name in this" scope"
            }
            doSomethingThatMightTakeABit(item)
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试添加标签,例如inner@scope.launch,编辑器会说“标签是多余的,因为它不能在 ''break''、''Continue'' 或 ''return'' 表达式中引用”

有谁知道这样做的好方法?

Ser*_*gey 5

如果我们需要从 lambda 表达式返回,我们必须对其进行标记并限定return。为了让您的案例从内部协程返回:

scope.launch {
    items.forEach { item ->
        scope.launch innerScope@ {
            if (itemFailsValidation(item)) {
                return@innerScope
            }
            doSomethingThatMightTakeABit(item)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我们可以简化您的情况并重写代码而不使用标签:

scope.launch {
    items.forEach { item ->
        if (!itemFailsValidation(item)) {
            scope.launch { doSomethingThatMightTakeABit(item) }
        }
    }
}

// OR

items.forEach { item ->
    if (!itemFailsValidation(item)) {
        scope.launch { doSomethingThatMightTakeABit(item) }
    }
}    
Run Code Online (Sandbox Code Playgroud)

如果您想等待所有协程完成并在 UI 线程上执行某些操作:

scope.launch(Dispatchers.Main) {
    processItemsInBackground()

    // update UI after processing is finished
}

suspend fun processItemsInBackground() = withContext(Dispatchers.IO) {
    // withContext waits for all children coroutines
    items.forEach { item ->
        if (!itemFailsValidation(item)) {
            launch { doSomethingThatMightTakeABit(item) }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)