Returning from inner nested coroutine by label Kotlin

Brandon Xia

Use case: I have a lot of operations that I want to happen asynchronously from the main thread but also in parallel with each other.

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)
         }
     }
}

If I try to add a label, like [email protected], editor says "Label is redundant, because it can not be referenced in either ''break'', ''continue'', or ''return'' expression"

Does anyone know a good way of doing this?

Sergey

If we need to return from a lambda expression, we have to label it and qualify the return. For your case to return from inner coroutine:

scope.launch {
    items.forEach { item ->
        scope.launch innerScope@ {
            if (itemFailsValidation(item)) {
                return@innerScope
            }
            doSomethingThatMightTakeABit(item)
        }
    }
}

But we can simplify your case and rewrite code without using a label:

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

// OR

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

If you want to wait for all coroutines to finish and do something on UI thread:

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) }
        }
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Kotlin coroutine list returning null value

分類Dev

Returning values from nested functions - JavaScript

分類Dev

Returning to main menu from another inner menu using c++

分類Dev

Kotlin return@forEach from nested forEach

分類Dev

How to access extension function from inner class[AsyncTask] in Kotlin?

分類Dev

Get inner-most elements from triple nested list Python

分類Dev

How to call Kotlin coroutine in composable function callbacks?

分類Dev

Kotlin alternative to Python's coroutine yield and send

分類Dev

Returning bool in nested function

分類Dev

Remove inner nested string with RegEx

分類Dev

Pandas inner merge/join returning all rows

分類Dev

SQL server inner join not returning Description

分類Dev

KeyNotFoundException vs returning null in Kotlin

分類Dev

Returning a promise nested in forEach, in a .then function

分類Dev

Nested array not returning correct data

分類Dev

Nested let blocks in Kotlin

分類Dev

KotlinのCOROUTINE_SUSPENDEDおよびsuspendCoroutineOrReturn

分類Dev

how to use Coroutine in kotlin to call a function every second

分類Dev

Does Kotlin's launch start a coroutine in the main or a background thread?

分類Dev

Suspending a Kotlin coroutine until a flow has a specific value

分類Dev

Rxjs nested subscribe with multiple inner subscriptions

分類Dev

Grouping a nested map based on keys of an inner map

分類Dev

Kotlin vs Java nested generics

分類Dev

nested let blocks in kotlin and valiadation

分類Dev

Returning ArrayList from AsyncTask

分類Dev

Returning Value from Actor

分類Dev

Returning a value from a Thread?

分類Dev

Angular4 returning Observable with nested Observables

分類Dev

While loop not returning correct output when nested

Related 関連記事

  1. 1

    Kotlin coroutine list returning null value

  2. 2

    Returning values from nested functions - JavaScript

  3. 3

    Returning to main menu from another inner menu using c++

  4. 4

    Kotlin return@forEach from nested forEach

  5. 5

    How to access extension function from inner class[AsyncTask] in Kotlin?

  6. 6

    Get inner-most elements from triple nested list Python

  7. 7

    How to call Kotlin coroutine in composable function callbacks?

  8. 8

    Kotlin alternative to Python's coroutine yield and send

  9. 9

    Returning bool in nested function

  10. 10

    Remove inner nested string with RegEx

  11. 11

    Pandas inner merge/join returning all rows

  12. 12

    SQL server inner join not returning Description

  13. 13

    KeyNotFoundException vs returning null in Kotlin

  14. 14

    Returning a promise nested in forEach, in a .then function

  15. 15

    Nested array not returning correct data

  16. 16

    Nested let blocks in Kotlin

  17. 17

    KotlinのCOROUTINE_SUSPENDEDおよびsuspendCoroutineOrReturn

  18. 18

    how to use Coroutine in kotlin to call a function every second

  19. 19

    Does Kotlin's launch start a coroutine in the main or a background thread?

  20. 20

    Suspending a Kotlin coroutine until a flow has a specific value

  21. 21

    Rxjs nested subscribe with multiple inner subscriptions

  22. 22

    Grouping a nested map based on keys of an inner map

  23. 23

    Kotlin vs Java nested generics

  24. 24

    nested let blocks in kotlin and valiadation

  25. 25

    Returning ArrayList from AsyncTask

  26. 26

    Returning Value from Actor

  27. 27

    Returning a value from a Thread?

  28. 28

    Angular4 returning Observable with nested Observables

  29. 29

    While loop not returning correct output when nested

ホットタグ

アーカイブ