7

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 :)

Nathan Horrigan
  • 763
  • 1
  • 9
  • 20
  • https://github.com/ReactiveX/RxAndroid/issues/348 might this help? What's your rx related imports? – fweigl Apr 22 '17 at 21:11
  • Thanks, @Ascorbin. That issue post did help and I realised that the library I was using imported 'rx.Observable', so I changed it to 'io.reactivex.Observable' but that hasn't solved the issue. – Nathan Horrigan Apr 22 '17 at 21:31

6 Answers6

8
  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.
Kiskae
  • 24,655
  • 2
  • 77
  • 74
  • Thanks for the clear explanation! What's weird is that I upgraded library to RxJava2 and the problem still persists. – Nathan Horrigan Apr 23 '17 at 09:46
  • RxJava and RxJava 2 use completely different packages, so if RxJava 1 is still available simply adding RxJava 2 as a dependency will change nothing. – Kiskae Apr 23 '17 at 12:23
  • Yep I Know, I switched imports from 'rx.Observable' to 'io.reactivex.Observable' also. Still no success :( – Nathan Horrigan Apr 24 '17 at 19:37
  • In case anyone else is receiving this error while using Retrofit, this post is helpful in resolving retrofit RxJava1 to RxJava2 errors https://stackoverflow.com/questions/43434073/unable-to-create-call-adapter-for-io-reactivex-observable – CrimsonX Nov 07 '19 at 20:31
7

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"
Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
5

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
buzatto
  • 9,704
  • 5
  • 24
  • 33
0

use

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

  • 1
    just a suggestion: When answering old question with multiple solutions with upvotes, try to provide some more details to understand the answer in context to the original question. Also provide the link to the documentation if possible. – Keval Langalia Feb 19 '21 at 02:01
0

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'
jenos kon
  • 504
  • 4
  • 7
0

Replace the following:

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

With:

implementation "io.reactivex.rxjava3:rxandroid:3.0.0"
OBrien Evance
  • 704
  • 5
  • 18