Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RXJava Ignore Error and continue in chain

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.

For example in this code

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

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

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

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

like image 543
Ambi Avatar asked Oct 25 '25 14:10

Ambi


2 Answers

I figured this out in Kotlin here (stackoverflow link)

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)
         }
     )
like image 111
methodsignature Avatar answered Oct 27 '25 04:10

methodsignature


I think you can use onErrorResumeNext operator.

              authDataManager
                    .getUserAccessToken(username, password)
                    .subscribeOn(Schedulers.io())
                    .doOnNext({
                        authDataManager.saveUserAccessToken(it)
                    })
                    .flatMap {
                        appDataManager.getStations()
                             .onErrorResumeNext(Observable.empty())
                    }
                    .doOnNext({
                        appDataManager.persistStations(it)
                    })
                    .flatMap {
                        appDataManager.getDriverInformation()
                    }
                    .doOnNext({
                        appDataManager.persistDriverInformation(it)
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            {
                                onLoginSuccess()
                            },
                            {
                                onLoginFailure(it)
                            }
                    )
like image 45
savepopulation Avatar answered Oct 27 '25 06:10

savepopulation