Ely*_*lye 27 kotlin kotlin-flow
以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) }
}
Run Code Online (Sandbox Code Playgroud)
我得到了错误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
Run Code Online (Sandbox Code Playgroud)
当我添加 @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) }
}
Run Code Online (Sandbox Code Playgroud)
我在collect的 lambda(函数value -> println(value)中出现错误,如下所示
Type mismatch.
Required:
FlowCollector<Int>
Found:
([ERROR : ]) ? Unit
Cannot infer a type for this parameter. Please specify it explicitly.
Run Code Online (Sandbox Code Playgroud)
我使用的是 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'
Run Code Online (Sandbox Code Playgroud)
我做错了什么导致我无法在 Android Studio 中编译示例代码?
Ely*_*lye 43
答案是,不,collect不仅仅是内部 kotlinx.coroutines API。错误消息具有误导性。
根据@ir42 的评论,add import kotlinx.coroutines.flow.collect 解决问题。
附加信息,为什么我没有选择collectLatest作为答案
collect并且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().collect { value -> println(value) }
}
Run Code Online (Sandbox Code Playgroud)
收集将产生
I'm not blocked 1
1
I'm not blocked 2
2
I'm not blocked 3
3
Run Code Online (Sandbox Code Playgroud)
根据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) }
}
Run Code Online (Sandbox Code Playgroud)
会产生
I'm not blocked 1
I'm not blocked 2
1
I'm not blocked 3
2
Run Code Online (Sandbox Code Playgroud)
Ris*_*esh 20
添加以下导入:
import kotlinx.coroutines.flow.collect
Run Code Online (Sandbox Code Playgroud)
在它几乎让我发疯之后,我终于找到了解决方案。由于这里的解决方案都不适合我,所以我遇到了一个特殊情况,我正在处理一个多模块项目,而collect()一直在各处抛出这个错误,甚至手动导入它也没有修复它。
对我来说解决这个问题的方法是显式地将协程依赖项添加到我的项目的所有模块 build.gradle 文件中,而不仅仅是应用程序的模块,甚至是那些不使用协程的模块。
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.2")
Run Code Online (Sandbox Code Playgroud)
对于版本1.6.0-RC3及更新版本:
注释@InternalCoroutinesApi已从所有collect()函数中删除(请参阅此 PR)。
这意味着我们不会再看到错误,也不需要导入
import kotlinx.coroutines.flow.collect
Run Code Online (Sandbox Code Playgroud)
因为(连同另一个 PR的更改)我们现在可以collect()直接使用成员函数。
| 归档时间: |
|
| 查看次数: |
2944 次 |
| 最近记录: |