Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava/RxBinding : how to handle errors on RxView

I am using RxJava and RxBindings for view in android. following is an example of what I am doing.

    RxView.clicks(btMyButton).flatMap(btn -> {
        // another observable which can throw onError.
        return Observable.error(null);
    }).subscribe(object -> {
        Log.d("CLICK", "button clicked");
    }, error -> {
        Log.d("CLICK", "ERROR");
    });

when I click on MyButton, I use flatMap to return another observable which is a network call and can return success or error. when it returns an error i handle it in error block. but I am not able to click the button again.

How can I handle the error and still be able to click on the button again?

like image 410
g.revolution Avatar asked Aug 19 '15 00:08

g.revolution


1 Answers

GreyBeardedGeek is spot on. To be quite explicit about one of your options you can use .materialize():

RxView.clicks(btMyButton).flatMap(btn -> {
        if (ok) 
            return someObservable.materialize();
        else 
            return Observable.error(new MyException()).materialize();
    }).subscribe(notification -> {
        if (notification.hasValue())
            Log.d("CLICK", "button clicked");
        else if (notification.isOnError())
             Log.d("CLICK", "ERROR");
    });

By the way don't pass null to Observable.error().

like image 110
Dave Moten Avatar answered Sep 27 '22 17:09

Dave Moten