Cant 'observeOn' main thread with RxKotlin
Asked Answered
T

6

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

Tutorial answered 22/4, 2017 at 21:6 Comment(2)
github.com/ReactiveX/RxAndroid/issues/348 might this help? What's your rx related imports?Phosgenite
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.Tutorial
S
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.
Sapiential answered 22/4, 2017 at 23:36 Comment(4)
Thanks for the clear explanation! What's weird is that I upgraded library to RxJava2 and the problem still persists.Tutorial
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.Sapiential
Yep I Know, I switched imports from 'rx.Observable' to 'io.reactivex.Observable' also. Still no success :(Tutorial
In case anyone else is receiving this error while using Retrofit, this post is helpful in resolving retrofit RxJava1 to RxJava2 errors #43434573Emeliaemelin
P
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"
Prostomium answered 19/12, 2019 at 15:57 Comment(0)
R
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
Roath answered 30/11, 2017 at 7:50 Comment(0)
M
0

use

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

Misbecome answered 18/2, 2021 at 22:5 Comment(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.Zannini
G
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'
Guitarist answered 15/11, 2022 at 16:18 Comment(0)
K
0

Replace the following:

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

With:

implementation "io.reactivex.rxjava3:rxandroid:3.0.0"
Kee answered 29/5, 2023 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.