1
votes

I am trying to implement the exponential back off retry logic with Alamofire in case server returns 500 due to some reason for some of my end points.

Is there a way that I can achieve this with Alamofire? Please guide with example.

Thanks in advance.

2

2 Answers

3
votes

Alamofire 5 beta 2 now includes support for automatic retry based on error conditions with customizable backoff. Take a look!

1
votes

Yes, it's pretty straightforward, although I don't believe Alamofire provides it natively. Since the library does allow you to make network requests, though, it's fairly straightforward to just retry with an exponential backoff if you fail with a 500 error. Concretely, in the completion block of your Alamofire requests, you check the status code, and if it's 500, retry after a delay. That delay should be stored in some singleton at the level of your network manager, and be increased along an exponential delay curve of your liking as failures occur. It should then be reset to the base delay when your request succeeds.