Kotlin Coroutines as RxJava Schedulers π
The main idea comes from Allow converting CoroutineDispatcher to RxJava scheduler π‘
Use extension function asScheduler()
at your target CoroutineDispatcher π
val disposable = Observable
.create(...)
.subscribeOn(Dispatchers.IO.asScheduler())
.observeOn(Dispatchers.Main.asScheduler())
.subscribe(...)
You can also specify a CoroutineScope that all coroutine jobs will run in it and canceled by it π
val disposable = Observable
.create(...)
.subscribeOn(Dispatchers.IO.asScheduler(yourScope))
.observeOn(Dispatchers.Main.asScheduler(yourScope))
.subscribe(...)
Attention:
subscribeOn
and observeOn
must run in same scope,
otherwise you need to call disposable.dispose()
after call yourScope.cancel()
π
At your top-level build.gradle
β¬οΈ
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
And then at your project build.gradle
β¬οΈ
dependencies {
// for RxJava2 users, use v2.0.3
implementation 'com.github.mthli:RxCoroutineSchedulers:v3.0.3'
}
Done π»
At your pom.xml
β¬οΈ
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.mthli</groupId>
<artifactId>RxCoroutineSchedulers</artifactId>
<!-- for RxJava2 users, use v2.0.3 -->
<version>v3.0.3</version>
</dependency>
</dependencies>
Done π»
Copyright 2020 Matthew Lee
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.