Sim*_*pps 1 android kotlin kotlin-coroutines kotlin-flow
我正在 kotlinlang.org 上阅读有关流程的文章:https ://kotlinlang.org/docs/flow.html
他们展示了下一个例子:
fun simple(): Flow<Int> = flow {
println("Flow started")
for (i in 1..3) {
delay(100)
emit(I)
}
}
fun main() = runBlocking<Unit> {
println("Calling simple function...")
val flow = simple()
println("Calling collect...")
flow.collect { value -> println(value) }
println("Calling collect again...")
flow.collect { value -> println(value) }
}
Run Code Online (Sandbox Code Playgroud)
他们说输出是:
Calling simple function...
Calling collect...
Flow started
1
2
3
Calling collect again...
Flow started
1
2
3
Run Code Online (Sandbox Code Playgroud)
因此,UI 线程似乎正在等待第一个flow.collect
函数完成,然后再继续打印“再次调用收集...”
我希望当第一个流程构建器运行时,系统将打印“再次调用收集...”,因此输出将是:
Calling simple function...
Calling collect...
Calling collect again...
Flow started
1
2
3
Flow started
1
2
3
Run Code Online (Sandbox Code Playgroud)
我缺少什么?
归档时间: |
|
查看次数: |
886 次 |
最近记录: |