Kotlin coroutine flow, firstOrNull only needs to be first, lastOrNull only needs to be last

Kotlin coroutine flow, firstOrNull only needs to be first, lastOrNull only needs to be last

 

        <!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core -->
        <dependency>
            <groupId>org.jetbrains.kotlinx</groupId>
            <artifactId>kotlinx-coroutines-core</artifactId>
            <version>1.7.3</version>
            <type>pom</type>
        </dependency>

 

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main(args: Array<String>) {
    runBlocking {
        val firstResult = load()
            .firstOrNull() //虽然发射很多数据,但只要第1次发射过来的数据。
        println(firstResult)

        val lasResult = load()
            .lastOrNull() //虽然发射很多数据,但只要最后1次发射过来的数据。
        println(lasResult)
    }
}

fun load() = flow {
    for (i in 1..5) {
        emit(i)
        delay(10)
    }
}

 

 

 

 

f66a04c0c41e46b0b8d0cec959067e90.png

 

 

 

 

The kotlin coroutine flow task ends unexpectedly without emitting data retryWhen onEmpty (5)_zhangphil's blog-CSDN blog 1. flow, emit, onCompletion, collect. kotlin coroutine flow retryWhen retry when the function function fails to load (3)_zhangphil's blog-CSDN blog. The kotlin coroutine flow retryWhen retry when the function function fails to load (3) Loading fails once, and retrying succeeds once. kotlin coroutine flow retryWhen retry when the function function fails to load (3)_zhangphil's blog-CSDN blog. The kotlin coroutine flow retryWhen retry when the function function fails to load (3) Loading fails once, and retrying succeeds once. https://blog.csdn.net/zhangphil/article/details/130115220

The kotlin coroutine flow retry function returns to retry after failure (4) _zhangphil's blog-CSDN blog 1. flow, emit, onCompletion, collect. kotlin coroutine flow retryWhen retry when the function function fails to load (3)_zhangphil's blog-CSDN blog. The kotlin coroutine flow retryWhen retry when the function function fails to load (3) Loading fails once, and retrying succeeds once. kotlin coroutine flow retry retryWhen (2)_zhangphil's blog - CSDN blog. kotlin coroutine flow retry retryWhen (2) Second, retryWhen. Failed to load the first time, retries succeeded twice. https://blog.csdn.net/zhangphil/article/details/130093111 kotlin coroutine flow retryWhen when the function function fails to load, retry (3)_zhangphil's blog-CSDN blog kotlin coroutine flow retryWhen when the function function fails to load Retry (3) failed to load once, retry 1 successfully. https://blog.csdn.net/zhangphil/article/details/130092299

kotlin coroutine flow retry retryWhen (2)_zhangphil's blog - CSDN blog kotlin coroutine flow retry retryWhen (2) Two, retryWhen. https://blog.csdn.net/zhangphil/article/details/130086523

kotlin coroutine flow filter map flowOn zip combine (1)_zhangphil's blog-CSDN blog 1. flow, emit, onCompletion, collect. Fourth, map, reorganize and rewrite data. Eight, conflate merge. Nine, debounce deduplication. Second, the function as a flow. https://blog.csdn.net/zhangphil/article/details/130084723

 

Guess you like

Origin blog.csdn.net/zhangphil/article/details/132492588