RXJava忽略错误并继续执行链 [英] RXJava Ignore Error and continue in chain

查看:744
本文介绍了RXJava忽略错误并继续执行链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RXJava链,可以在其中抛出错误. 我希望链继续进行,并忽略错误并仅针对某些特定错误到达订阅.

I have a RXJava chain where an error can be thrown. I would like the chain to continue and ignore the error and reach the subscribe, for only some specific errors.

例如此代码中的

authDataManager
                    .getUserAccessToken(username, password)
                    .subscribeOn(Schedulers.io())
                    .doOnNext({
                        authDataManager.saveUserAccessToken(it)
                    })
                    .flatMap {
                        appDataManager.getStations()
                    }
                    .doOnNext({
                        appDataManager.persistStations(it)
                    })
                    .flatMap {
                        appDataManager.getDriverInformation()
                    }
                    .doOnNext({
                        appDataManager.persistDriverInformation(it)
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            {
                                onLoginSuccess()
                            },
                            {
                                onLoginFailure(it)
                            }
                    )

如果appDataManager.getStations()抛出错误,我仍然想继续并到达onLoginSuccess()方法.

if appDataManager.getStations() throws an error, I still want to continue and reach the onLoginSuccess() method.

但是,如果getUserAccessToken(username, password)失败,则应调用onLoginFailure.

However if getUserAccessToken(username, password) fails, onLoginFailure should be called.

我尝试在平面图及其内部添加onErrorResumeNext()和onExceptionResumeNext(),但是如果这样做,链将退出并且不会继续并到达订阅

I have tried to add onErrorResumeNext() and onExceptionResumeNext() after the flatmaps and inside them, but if I do that, the chain just exits and doesn't continue and reach the subscribe

推荐答案

我在Kotlin中发现了这个问题

I figured this out in Kotlin here (stackoverflow link)

可以通过结合使用映射,过滤,可选和onErrorResumeNext来完成.我认为这与当前版本的RxJava一样优雅.

It can be done by using a combination of mapping, filtering, optionals, and onErrorResumeNext. I think this is about as graceful as it gets with the current version of RxJava.

authDataManager
    .getUserAccessToken(username, password)
    .subscribeOn(Schedulers.io())
    .doOnNext({
        authDataManager.saveUserAccessToken(it)
    })
    .flatMap {
        appDataManager.getStations()
            .map(stations -> Single.just(Optional.of(stations))
            .onErrorResumeNext(error -> Single.just(Optional.empty()))                    
     }
     .filter(optional -> optional.isPresent())
     .map(stations -> optional.get())
     .doOnNext({
         appDataManager.persistStations(it)
     })
     .flatMap {
         appDataManager.getDriverInformation()
     }
     .doOnNext({
         appDataManager.persistDriverInformation(it)
     })
     .observeOn(AndroidSchedulers.mainThread())
     .subscribe(
         {
             onLoginSuccess()
         },
         {
             onLoginFailure(it)
         }
     )

这篇关于RXJava忽略错误并继续执行链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆