kotlin channel使用注意点
kotlinx.coroutines.channels.ClosedSendChannelException: Channel was closed
at kotlinx.coroutines.channels.Closed.getSendException(AbstractChannel.kt:1107)
at kotlinx.coroutines.channels.AbstractSendChannel.helpCloseAndResumeWithSendException(AbstractChannel.kt:230)
at kotlinx.coroutines.channels.AbstractSendChannel.access$helpCloseAndResumeWithSendException(AbstractChannel.kt:19)
at kotlinx.coroutines.channels.AbstractSendChannel.sendSuspend(AbstractChannel.kt:220)
at kotlinx.coroutines.channels.AbstractSendChannel.send(AbstractChannel.kt:136)
at kotlinx.coroutines.channels.ChannelCoroutine.send(Unknown Source:2)
因为 channel在另一个flow/channel里, 而kotlin的 job与channel 都是带 group概念的, 一个顶层job/channel关闭,会导致在其中的其他 也关闭
flow/channelFlow不能用时,可以使用 callbackFlow
https://blog.csdn.net/androiddevs/article/details/116078525
channelFlow 可以在不同的 CoroutineContext 或协程之外使用 offer(trySend) 方法发送数据。但一定要在block块内添加
awaitClose { channel.close(Throwable("完成")) }
最新文章
- java之ubuntu12.04 开发环境配制
- 6-02使用SQL语句向表中插入数据
- .net windows 服务创建、安装、卸载和调试
- 存在网路的情况下重命名SDE中数据图层错误(The orphan junction feature class cannot be renamed)
- Web性能压力测试工具之Apache AB 详解
- uva 10655 - Contemplation! Algebra
- oracle设备
- java_Eclipse自动生成作者、日期注释等功能设置_导入 xml方式
- (笔记):组合and继承之访问限制(一)
- php提供的sapi有哪些?CGI、FastCGI、php-fpm、php-cgi解释
- (2)Deep Learning之线性单元和梯度下降
- 十款不容错过的Swift iOS开源项目及介绍
- js进度条小事例
- Win7 安装bundle
- getParameter() getInputStream()和getReader() 区别 这三种方法是有冲突的,因为流只能被读一次。
- Python常用字符编码
- poj 3683(2-SAT+SCC)
- LeetCode--255--用队列实现栈(java版)
- jupyterlab notebook区别
- HDOJ 1814 Peaceful Commission