[英]Rxjava with kotlin coroutines
I am in need that i need to start the coroutines from rxkotlin chain, But am i not sure whether this is right or wrong, to start a coroutines from the rx chain i use runblocking
to start the suspend methods我需要从 rxkotlin 链启动协程,但我不确定这是对还是错,从 rx 链启动协程我使用运行
runblocking
来启动挂起方法
Example例子
Single.just(someOperation())
.map{
someMethod(it)
}
.flatMap{
startCoroutines(suspend { someOpeartions() } ) // i will be starting the coroutines here
}
Coroutines协程
fun startCoroutines(suspendingObj : suspend () -> Any){
runBlocking(newFixedThreadPoolContext(1,"Thread")){
suspendingObj.invoke()
}
}
Is this above code is correct way of doing it or is there is any other way to achieve this ?上面的代码是正确的做法还是有其他方法可以实现这一点? Can anyone help me out with this
谁能帮我解决这个问题
This code chunk is fundamentally wrong.这个代码块从根本上是错误的。
flatMap
with observeOn
and pass any Scheduler
you want (like IO).flatMap
与observeOn
和传递任何Scheduler
想要(比如IO)。Threads
, because creating Threads
is a very expensive operation. Threads
,因为创建Threads
是一项非常昂贵的操作。 And your function startCoroutines
creates a new thread for every operation which makes no sense and potentially will cause overflow
.startCoroutines
为每个操作创建一个新线程,这没有意义并且可能会导致overflow
。 You can read more about it here: Difference between a "coroutine" and a "thread"?runBlocking
.runBlocking
之前,您应该始终尝试找到更好的系统设计。 Blocking threads is never a good idea.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.