Rxjava 与 kotlin 协程

Rxjava with kotlin coroutines

我需要从 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()
  }
}

上面的代码是正确的方法还是有其他方法可以实现? 谁能帮我解决这个问题

这段代码块根本上是错误的。

  1. 在你的情况下,确实没有必要使用协程,因为你可以轻松地在 flatMap 之前将 Rx 线程更改为 observeOn 并传递任何你想要的 Scheduler (如 IO) .
  2. Kotlin 协程旨在避免 Threads,因为创建 Threads 是一项非常昂贵的操作。并且您的函数 startCoroutines 为每个没有意义的操作创建一个新线程,并且可能会导致 overflow。您可以在这里阅读更多相关信息:Difference between a "coroutine" and a "thread"?
  3. 在决定使用 runBlocking 之前,您应该始终尝试找到更好的系统设计。阻塞线程从来都不是一个好主意。