【问题标题】:Is there a Kotlin coroutine channel that behaves like XStream Stream?有没有像 XStream Stream 一样的 Kotlin 协程通道?
【发布时间】:2018-07-06 10:22:59
【问题描述】:
【问题讨论】:
标签:
kotlin
kotlinx.coroutines
【解决方案1】:
不,没有这样的频道,请随时create an issue。
您可以采用的最接近的解决方法是在 ConflatedBroadcastChannel 上提供一个扩展,这将丢弃现有元素:
fun <T> ConflatedBroadcastChannel<T>.subscription(): ReceiveChannel<T> {
val subscription = openSubscription()
subscription.poll()
return subscription
}
从消费者的角度来看,这个订阅的语义和XStream是一样的,但是它不符合“没有缓冲区”的部分:发送的元素会被保存在内存中(~不能被垃圾回收) 直到下一个 send 被调用。