嘿,我是 Kotlin Flow 的新手。我正在使用MutableStateFlow在流中添加值、附加值、附加值。
1.我在不列印任何東西println()后嘗試使用。collectLatest有人可以解釋一下為什么這不起作用。
2.有人可以指導我如何add,append并prepend在流程中列出。
例如
附加
我的流串列值是[1, 2, 3, 4, 5]并且想要添加[6, 7, 8, 9, 10]。
輸出將是
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
前置
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]after append的相同串列。我想在串列中添加前綴,值為[-5, -4, -3, -2, -1].
輸出將是
[-5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
我正在嘗試,但我不明白為什么此功能不起作用。
主.kt
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
suspend fun main(args: Array<String>) {
val flow = MutableStateFlow<List<Int>>(emptyList())
val updateList = listOf<Int>(-5, -4, -3, -2, -1)
for (i in 1..10) {
flow.value = i
}
flow.collectLatest {
println(it)
}
println("After Update")
flow.value = flow.value updateList
flow.collectLatest {
println(it)
}
}

uj5u.com熱心網友回復:
SharedFlow 或 StateFlow 沒有結束,除非它被取消。呼叫collectorcollectLatest有點像創建一個while(true)回圈。它永遠不會完成迭代,因為它總是有可能發出另一個值。
在檔案中,您可以在以“ Shared flow never completes. ”開頭的部分閱讀它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/411491.html
標籤:
