Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 223
Nathan Horrigan Avatar asked Apr 22 '17 21:04

Nathan Horrigan


1 Answers

  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.
like image 107
Kiskae Avatar answered Sep 27 '22 23:09

Kiskae