方法对Kotlin Coroutine版本的升级提出了"map“和"filter”两种方法。由于某些原因,我需要升级Coroutine版本,所以早期版本的Kotlin coroutine代码运行得非常好。但对此有很多想法。
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.filter
import kotlinx.coroutines.channels.map
object EventBus {
@ObsoleteCoroutinesApi
val bus: BroadcastChannel<Any> = BroadcastChannel(1)
@ObsoleteCoroutinesApi
fun send(o: Any) {
CoroutineScope(Dispatchers.IO).launch {
bus.send(o)
}
}
@ObsoleteCoroutinesApi
inline fun <reified T> asChannel(): ReceiveChannel<T> {
return bus.openSubscription().filter { it is T }.map { it as T }
}
}````
Can we replace the above usage of Map and Filter? Any help would be greatly appreciated.发布于 2022-05-28 21:16:55
您应该严肃对待注释@ObsoleteCoroutinesApi,因为类BroadcastChannel已经完全不推荐使用SharedFlow或MutableSharedFlow;也可以看到这个示例。
这篇博文应该支持理解和迁移:共享流、广播频道。
https://stackoverflow.com/questions/72419231
复制相似问题