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