gpt4 book ai didi

Kotlin ConflatedBroadcastChannel.offer() 不起作用?

转载 作者:行者123 更新时间:2023-12-02 13:17:51 24 4
gpt4 key购买 nike

我通过 MyRepository.myConflatedChannel.offer(myvalue) 发送一个值.

然后我希望在 collect { } 收到它或 onEach { }我的 ViewModel 中的块。但是,这两个函数都不会被调用。就好像什么都没有传递到 ConflatedBroadcastChannel。

有没有人见过类似的问题?

最佳答案

确保您正确处理接收值。

如果您使用 ConflatedBroadcastChannel,则可以使用 OpenSubscription获得 ReceiveChannel或者您可以将其表示为流(使用 asFlow )。

请注意 consumeconsumeEach是终端,它们执行一个 Action ,然后在块执行后取消 channel 。见 this .

第一种情况:

val receivingChannel = MyRepository.myConflatedChannel.openSubscription()
// then you can consume values using for example a for loop, e.g.:

launch {
for (value in receivingChannel) {
// do something
}
}

第二种情况:
val receivingFlow = MyRepository.myConflatedChannel.asFlow()

launch {
receivingFlow.collect {
// do something
}
}

关于Kotlin ConflatedBroadcastChannel.offer() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61690683/

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