gpt4 book ai didi

kotlin - Kotlin Flow 的 Collect 是否只是内部 kotlinx.coroutines API?

转载 作者:行者123 更新时间:2023-12-03 14:35:54 24 4
gpt4 key购买 nike

https://kotlinlang.org/docs/reference/coroutines/flow.html#flows-are-cold 中的直接例子为例

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) }
}
我在 collect 上收到错误消息.
This is an internal kotlinx.coroutines API that should not be used from outside of kotlinx.coroutines. No compatibility guarantees are provided.It is recommended to report your use-case of internal API to kotlinx.coroutines issue tracker, so stable API could be provided instead
当我添加 @InternalCoroutinesApi
@InternalCoroutinesApi
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) }
}
我在 collect 中遇到错误的 lambda( value -> println(value 的函数)如下
Type mismatch.
Required:
FlowCollector<Int>
Found:
([ERROR : ]) → Unit
Cannot infer a type for this parameter. Please specify it explicitly.
我使用的是 Kotlin 1.4.21 版。
    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.2"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.2'
我做错了什么导致我无法在 Android Studio 中编译示例代码?

最佳答案

答案是,否,collect不仅仅是内部 kotlinx.coroutines API。错误消息具有误导性。
根据@ir42 的评论,add import kotlinx.coroutines.flow.collect 解决这个问题。
附加信息,为什么我没有选择 collectLatest作为答案collectcollectLatest是不同的。
使用这个例子

fun simple(): Flow<Int> = flow { // flow builder
for (i in 1..3) {
delay(100) // pretend we are doing something useful here
emit(i) // emit next value
}
}

fun main() = runBlocking<Unit> {
// Launch a concurrent coroutine to check if the main thread is blocked
launch {
for (k in 1..3) {
println("I'm not blocked $k")
delay(100)
}
}
// Collect the flow
simple().collect { value -> println(value) }
}
收集将产生
I'm not blocked 1
1
I'm not blocked 2
2
I'm not blocked 3
3
根据 https://kotlinlang.org/docs/reference/coroutines/flow.html
但是 collectLatest
fun simple(): Flow<Int> = flow { // flow builder
for (i in 1..3) {
delay(100) // pretend we are doing something useful here
emit(i) // emit next value
}
}

fun main() = runBlocking<Unit> {
// Launch a concurrent coroutine to check if the main thread is blocked
launch {
for (k in 1..3) {
println("I'm not blocked $k")
delay(100)
}
}
// Collect the flow
simple().collectLatest { value -> println(value) }
}
会产生
I'm not blocked 1
I'm not blocked 2
1
I'm not blocked 3
2

关于kotlin - Kotlin Flow 的 Collect 是否只是内部 kotlinx.coroutines API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65559153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com