作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要从 rxkotlin 链启动协程,但我不确定这是对还是错,要从 rx 链启动协程,我使用 runblocking
启动挂起方法
示例
Single.just(someOperation())
.map{
someMethod(it)
}
.flatMap{
startCoroutines(suspend { someOpeartions() } ) // i will be starting the coroutines here
}
协程
fun startCoroutines(suspendingObj : suspend () -> Any){
runBlocking(newFixedThreadPoolContext(1,"Thread")){
suspendingObj.invoke()
}
}
上面的代码是正确的做法还是有其他方法可以实现?谁能帮我解决这个问题
最佳答案
这个代码块从根本上是错误的。
observeOn
在 flatMap
之前轻松更改 Rx 线程,并传递任何 Scheduler
你想要(比如 IO)。Threads
,因为创建Threads
是一项非常昂贵的操作。并且您的函数 startCoroutines
为每个操作创建一个新线程,这没有任何意义并且可能会导致 溢出
。您可以在这里阅读更多相关信息:Difference between a "coroutine" and a "thread"? runBlocking
之前,您应该始终尝试找到更好的系统设计。阻塞线程从来都不是一个好主意。关于android - Rxjava 与 kotlin 协程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57105778/
在我的设置中,我试图有一个界面 Table继承自 Map (因为它主要用作 map 的包装器)。两个类继承自 Table - 本地和全局。全局的将有一个可变的映射,而本地的将有一个只有本地条目的映射。
Rust Nomicon 有 an entire section on variance除了关于 Box 的这一小节,我或多或少地理解了这一点和 Vec在 T 上(共同)变体. Box and Vec
我是一名优秀的程序员,十分优秀!