简体   繁体   English

带有 kotlin 协程的 Rxjava

[英]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.这个代码块从根本上是错误的。

  1. In your case it is really not necessary to use Coroutines, because you can easily change Rx thread before flatMap with observeOn and pass any Scheduler you want (like IO).在你的情况下,它确实是使用协同程序没有必要的,因为你可以很容易地改变之前的Rx线flatMapobserveOn和传递任何Scheduler想要(比如IO)。
  2. Kotlin Coroutines were designed to avoid Threads , because creating Threads is a very expensive operation. Kotlin 协程旨在避免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"?您可以在此处阅读更多相关信息: “协程”和“线程”之间的区别?
  3. You should always try to find a better system design before deciding using runBlocking .在决定使用runBlocking之前,您应该始终尝试找到更好的系统设计。 Blocking threads is never a good idea.阻塞线程从来都不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有Kotlin协程的RxJava2 - RxJava2 with Kotlin coroutines Kotlin 协程中是否有 RxJava 主题的类似物? - Is there analogue for RxJava Subject in Kotlin Coroutines? 使用 RxJava 或 kotlin 协程的 Android ViewState - Android ViewState using RxJava or kotlin coroutines 从 RxJava2 迁移到 Kotlin 协程 - Migrating from RxJava2 to Kotlin Coroutines 带有bindservice和kotlin的Android Things-onResume和onPause或Coroutines或RxJava - Android Things with bindservice and kotlin - onResume and onPause Or Coroutines Or RxJava 使用流替换 RxJava 在 Kotlin 协程中单击侦听器 - click listener in Kotlin coroutines using flow replacing RxJava Completable.create 在 Kotlin Coroutines- Android 中等效于 RXJava - Completable.create of RXJava equivalent in Kotlin Coroutines- Android 使用 RxJava、RxKotlin 甚至 Kotlin 协程而不是 Interfaces for On Click 监听器有什么好处? - What is the benefit of using RxJava, RxKotlin, or Even Kotlin coroutines instead of Interfaces for On Click listener? 你能在同一个项目中同时使用 Kotlin Coroutines 和 RxJava 吗? - Can you use both Kotlin Coroutines and RxJava in the same project, at the same time? 协程中RxJava的share()的替代方法是什么? - what is the alternative in coroutines for `share()` of `RxJava`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM