I'm trying to implement an alamofire call with max retries. Code is below:
RxAlamofire.request(.post, URL, parameters: parameters, encoding: JSONEncoding.default)
.observeOn(MainScheduler.instance)
.retryWhen { (errors: Observable<Error>) in
return errors.flatMapWithIndex { (e, a) -> Observable<Int64> in
if a >= self.RETRY_COUNT - 1 {
return Observable.error(e)
}
print("Error: delay server call retry by \(a+1) second(s)")
return Observable<Int64>.timer(RxTimeInterval(a+1), scheduler: MainScheduler.instance)
}
}
.subscribe(
onNext: {
(result) in
print("I get here when retrying...")
},
onError: { (error) in
print(error)
}
)
.addDisposableTo(self.disposeBag)
Unfortunately, on retrying, I get into the onNext block in subscribe - I don't want to get there until I have a result. (The onError gives an error after max retries is exceeded as expected). Please can someone help?
I have tried to reproduce your scenario, but the following code does not trigger the onNext closure in the subscription upon an error. I have rewritten your code slightly, my example always errors out. I am using RxSwift 4.0.0.
This results in the following output.
2018-03-16 09:05:16.921: after -> subscribed 2018-03-16 09:05:16.924: prior -> subscribed 2018-03-16 09:05:16.924: prior -> Event error(blok) Error: delay server call retry by 1 second(s) 2018-03-16 09:05:16.925: prior -> isDisposed 2018-03-16 09:05:17.926: prior -> subscribed 2018-03-16 09:05:17.926: prior -> Event error(blok) 2018-03-16 09:05:17.927: after -> Event error(blok) 2018-03-16 09:05:17.928: after -> isDisposed 2018-03-16 09:05:17.928: prior -> isDisposed