JakeWharton / RxBinding

RxJava binding APIs for Android's UI widgets.
Apache License 2.0
9.68k stars 971 forks source link

XX.subscribeOn(Schedulers.io()) not working #368

Closed Sebsen closed 7 years ago

Sebsen commented 7 years ago

Hello, first of all I want to say that your Library looks great and I'm glad being able to use it.

Unfortunately I'm getting an error on following line of code:

RxView.clicks(_btn_loadWithRxAndroid)

    .subscribeOn(Schedulers.io())    // Error occures here 

    .observeOn(AndroidSchedulers.mainThread())

    .subscribe(...);

It says: "subscribeOn( rx. Scheduler) in Observable cannot be applied to ( io.reactivex. Scheduler)"

Am I doing something wrong or is it still under development? I hope you can clear me up or have another workaround for me.

I have every of the rxbinding dependencies included like:

compile 'com.jakewharton.rxbinding:rxbinding:1.0.1'

except the leanback-v17 one.

JakeWharton commented 7 years ago

There's two problems here.

The first, as the exception message indicates, is that you are using the wrong Schedulers import. io.reactivex.Scheduler is from RxJava 2.x whereas rx.Scheduler is from 1.x. You need to import the 1.x version of Schedulers or fully-qualify the type (rx.Schedulers).

The second is that every observable in this library requires that you subscribe on the main thread and no other. In your example this amounts to just deleting both subscribeOn and observeOn allowing the stream to subscribe synchronously on the main thread and also observe items synchronously on the main thread.

trinadhkoya commented 7 years ago

@JakeWharton you mean there is no Subscription in Rx2 right ?

OrdonTeam commented 7 years ago

There is but in different package. If you want to make a network call on click you have to use observeOn twice. It is because (as Jake said) RxBinding require subscriptions to execute on main thread.

RxView.clicks(_btn_loadWithRxAndroid)
    .observeOn(Schedulers.io())
    .switchMap{ call() } 
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(...);
thiyagesh6m commented 6 years ago

Hi Anyone can please help me on this Attempt to invoke virtual method 'io.reactivex.Single io.reactivex.Single.subscribeOn(io.reactivex.Scheduler)' on a null object reference at com.dt.admin.newdriver.ui.login.LoginPresenter.onServerLoginClick(LoginPresenter.java:56)

artem-zinnatullin commented 6 years ago

That very much likely indicates null pointer exception in your code.

Moreover it doesn't seem to be related to RxBinding because it's illegal to subscribe on non-Main thread scheduler for all UI (afaik) bindings. (unless view was created on non-main thread originally, but that's advanced stuff)