Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatmap Single into Observable and ignore errors #3472

Closed
passsy opened this issue Oct 27, 2015 · 5 comments
Closed

Flatmap Single into Observable and ignore errors #3472

passsy opened this issue Oct 27, 2015 · 5 comments

Comments

@passsy
Copy link
Contributor

passsy commented Oct 27, 2015

I have UI observable which should work as long as my UI lives. When my UI emits I create a network request which could be an Observable or a Single. A Single fits best for a network call.

RxView.clicks(myButton)
    .flatMap(v - > networkRequest())
    .subscribe(data -> showSomething(data),
            e -> {
                // should only be called when the RxView.clicks() throws. 
                // should not be called for network errors
            });

When using an Observable I would use onErrorResumeNext returning Observable.empty() to prevent errors coming from the network request going into my UI Observable and calling onError because the UI Observable should life forever:

Observable networkRequest() {
    mApiService.fireActionObservable()
        .onErrorResumeNext(throwable -> {
            // somehow handle error here
            return Observable.empty();
     });
}

This, in my opinion, elegant way does not work for Single because no Single.empty() exists.

Single networkRequest() {
    mApiService.fireActionSingle()
        .onErrorResumeNext(throwable -> {
            // somehow handle error here
            return Single.empty(); // <-- does not exist. I have to call success or error :/
     });
}

Single.onErrorResumeNext is btw only available in 2.x

Converting my Single to an Observable seems wrong, because the network request is a Single!

@passsy passsy changed the title Flatmap Single into observable and handle error gracefully Flatmap Single into Observable and handle error gracefully Oct 27, 2015
@passsy passsy changed the title Flatmap Single into Observable and handle error gracefully Flatmap Single into Observable and ignore error Oct 27, 2015
@passsy passsy changed the title Flatmap Single into Observable and ignore error Flatmap Single into Observable and ignore errors Oct 27, 2015
@akarnokd
Copy link
Member

Single.empty() doesn't make sense because Single has to emit either an onSuccess or an onError. The best you can do is to have a Single.just() with a normal value that represents emptiness.

Generally, this is why I'm skeptic about Single and Completable because Observable can "emulate" both and the overhead in true async use is negligible.

@passsy
Copy link
Contributor Author

passsy commented Oct 27, 2015

My current solution wraps my Singles:

    public static <T> Observable<T> onErrorResumeNext(final Single<T> single,
            final Observable<? extends T> resumeSequence) {
        return single.toObservable().onErrorResumeNext(resumeSequence);
    }

    public static <T> Observable<T> onErrorResumeNext(final Single<T> single,
            final Func1<Throwable, ? extends Observable<? extends T>> resumeFunction) {
        return single.toObservable().onErrorResumeNext(resumeFunction);
    }
RxView.clicks(myButton)
    .flatMap(v - > onErrorResumeNext(networkRequest(),
            throwable -> {
            // somehow handle error here
            return Observable.empty();
     }))
    .subscribe(data -> showSomething(data),
            e -> {
                // only called when the RxView.clicks() throws
            });

@benjchristensen what happened to the initial idea of heaving a Single with 3 final states?

A Single will always behave in one of 3 ways:

  1. respond with an error
  2. never respond
  3. respond with a success

I was hoping a Single is a Observable where I don't have to care about multiple items. Nothing more. Like Observable#single() but with zero item support and type safetyness.

@akarnokd
Copy link
Member

@artem-zinnatullin I'm starting to loose what is implemented where. Do you work on Single.onErrorResumeNext(Func1)?

@artem-zinnatullin
Copy link
Contributor

@akarnokd damn… will submit PR today/tomorrow!

@MalekKamel
Copy link

My solution:
RxView.clicks(myButton)
.flatMap(v - > networkRequest())
.compose(exceptionHandling())
.subscribe(data -> showSomething(data),
e -> {
// Now you should remove this lambda as it will never be called.
});
Final:
RxView.clicks(myButton)
.flatMap(v - > networkRequest())
.compose(exceptionHandling())
.subscribe(data -> showSomething(data));
exceptionHandling:
protected ObservableTransformer<T, T> exceptionHandling() {
return observable ->
observable
.doOnError(t ->{ /* handle error here */})
.retry(); // Ignore error to keep subscription.
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants