简体   繁体   English

带有Kotlin协程的RxJava2

[英]RxJava2 with Kotlin coroutines

I'm using both in my app. 我在我的应用程序中同时使用两者。 Kotlin Coroutines are used for single interactions, such as a DB or an API call. Kotlin Coroutines用于单个交互,例如DB或API调用。 But also I'm using RxJava Flowable to observe sensor events. 但是我也正在使用RxJava Flowable来观察传感器事件。

Kotlin Coroutines has dispatches , and RxJava has shedulers . Kotlin Coroutinesdispatches ,而RxJavashedulers The idea of them is to provide maximally efficient thread pools, which size depends on hardware configuration. 它们的想法是提供最大效率的线程池,该线程池的大小取决于硬件配置。

So if I use both simultaneously, I can have, for example, 8 running threads, 4 in Dispatchers.Default and 4 in Shedulers.computation , instead of 4 threads. 因此,如果同时使用这两个线程,则可以有8个正在运行的线程,而Dispatchers.Default中有4个线程,而Shedulers.computation有4个线程,而不是4个线程。

So my question is 所以我的问题是

Is there a way use Kotlin Dispatchers as RxJava shedulers or vise versa? 有没有办法将Kotlin Dispatchers用作RxJava shedulers ,反之亦然?

Or I should define my own executors , and build dispatchers and shedulers from them? 还是我应该定义自己的executors ,并shedulers建立dispatchersshedulers

您可以使用kotlinx-coroutines-rx2扩展库( https://github.com/Kotlin/kotlinx.coroutines/tree/master/reactive/kotlinx-coroutines-rx2 )....尤其是Scheduler.asCoroutineDispatcher

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM