Nathan Horrigan
Nathan Horrigan

Reputation: 813

Cant 'observeOn' main thread with RxKotlin

I'm trying to observe observable on main thread by using:

    // Kotlin Code
    Observable
      .observeOn(AndroidSchedulers.mainThread())

but I'm getting following error:

    Type Mismatch:
      Required: rx.Scheduler!
      Found: io.reactivex.Scheduler!

The Observable I'm subscribing to is from a Library that is written in Java and therefore uses RxJava.

Am i being stupid and missing something? I'm puzzeled :$

Thanks in advance :)

Upvotes: 7

Views: 7163

Answers (6)

Ece Akilli
Ece Akilli

Reputation: 1

Replace the following:

implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

With:

implementation "io.reactivex.rxjava3:rxandroid:3.0.0"

Upvotes: 0

jenos kon
jenos kon

Reputation: 564

Try to use these dependencies it works for me :

implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.x.x'

Upvotes: 0

please include this in your gradle import

rxandroid_version="2.0.1"

implementation "io.reactivex.rxjava2:rxandroid:$rxandroid_version"

add this to your project

import io.reactivex.android.schedulers.AndroidSchedulers

Upvotes: 5

Mojtaba Sheikhy
Mojtaba Sheikhy

Reputation: 1

use

.subscribeOn(io.reactivex.rxjava3.schedulers.Schedulers.io())

Upvotes: 0

Sana Ebadi
Sana Ebadi

Reputation: 7220

1.add :

   //RX JAVA
    implementation "io.reactivex.rxjava3:rxjava:3.0.0-RC6"

    implementation "io.reactivex.rxjava2:rxandroid:2.0.2"

2.Write this code :

  val taskObservable: Observable<Task> = Observable.fromIterable(DataSource.createTaskList())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())

    }

3.add these imports :

import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Function
import io.reactivex.schedulers.Schedulers

NOTE :

if you are using KOTLIN, So it is better to use RX Kotlin! just add two lines in your build.gradle :

//rxkotlin
    implementation "io.reactivex.rxjava2:rxkotlin:2.4.0"
    implementation "io.reactivex.rxjava2:rxandroid:2.0.2"

Upvotes: 7

Kiskae
Kiskae

Reputation: 25603

  Required: rx.Scheduler!

Required means the signature is Observable.observeOn(rx.Scheduler)

  Found: io.reactivex.Scheduler!

Found means the signature is io.reactivex.Scheduler AndroidSchedulers.mainThread()

This means that the Observable is a RxJava 1 observable, while the RxAndroid version used is built for RxJava 2. Since you mentioned that the observable is provided by a library it means that library is built using RxJava 1.

You have 3 options to fix this:

  1. Figure out if the library in question has an RxJava 2 version, or contribute those updates to the project yourself.
  2. Use akarnokd/RxJava2Interop to convert the old Observable to RxJava 2. (RxJavaInterop.toV2Observable(Observable);)
  3. Switch the other dependencies back to RxJava 1.

Upvotes: 8

Related Questions