[英]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 Coroutines
已dispatches
,而RxJava
已shedulers
。 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
asRxJava shedulers
or vise versa?有没有办法将
Kotlin Dispatchers
用作RxJava shedulers
,反之亦然?Or I should define my own
executors
, and builddispatchers
andshedulers
from them?还是我应该定义自己的
executors
,并shedulers
建立dispatchers
和shedulers
?
您可以使用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.