1

I`m struggling to retry my rxjava Single call after another network call is done in doOnError:

restApi.getStuff()
       .subscribeOn(Schedulers.io())
       .observeOn(AndroidSchedulers.mainThread())
       .doOnError {
            getRefreshToken(it, AuthenticationManager.Callback{
                retry(1)
            })
       }
       .subscribeBy(
             onSuccess = { response ->},
             onError = { throwable ->}
       )

But the retry method cannot be invoked inside the doOnError method. Do you have any other ideas?

bleo
  • 219
  • 2
  • 8

2 Answers2

0

Eventually I used a different approach with creating an Interceptor for token authorization (@Skynet suggestion led me to it). Here is more info about it: Refreshing OAuth token using Retrofit without modifying all calls

bleo
  • 219
  • 2
  • 8
-1

if you want to check the response and then retry you should try this:

  restApi.getStuff()
       .subscribeOn(Schedulers.io())
       .observeOn(AndroidSchedulers.mainThread())
       .retryWhen(errors -> errors.flatMap(error -> {
          // for some exceptions
          if (error instanceof IOException) {
               return Observable.just(null);
          }

         // otherwise
         return Observable.error(error);
         })
         )

otherwise

restApi.getStuff()
       .subscribeOn(Schedulers.io())
       .observeOn(AndroidSchedulers.mainThread())
       .retry()

from the docs, retry() responds to onError. link

stallianz
  • 176
  • 1
  • 17
  • I solved my problem already in another way, but your solutions gives me: 'Unnresolved reference: errors' – bleo Oct 25 '18 at 10:06
  • This is incomplete. The question is not how to retry upon error, but how to chain an authentication token renew and then retry upon success. Or more generally, how to chain a call before actually retrying. – Gabriel Vasconcelos Jul 19 '19 at 16:17