Alamofire, Session Manager limit retry count
<p>I'm using Alamofire's SessionManager to make requests to my API services. </p>

<p>Is there a way to limit the retry counts in Alamofire? </p>

<p>I'm using the new RequestAdapter and RequestTrier Protocols to provide a retry mechanims in Alamofire v4.</p>

<p>I want to limit retries up to 5. If after 5 Retries a new request shouldn't be done.</p>

<pre><code>class VMRetrier: RequestAdapter, RequestRetrier {

public func adapt(_ urlRequest: URLRequest) throws -&gt; URLRequest {
    return urlRequest;
}

func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {

    if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 401 {
        VMLogger.shared.debug(message: "Trying to get a token")
        let _ = AuthService.shared.login(email: "a@a.com", password: "test34")
                           .then(execute: { (response) in
                                completion(true, 0.0)
                           })
    }
    else {
        completion(false, 0.0)
    }
} }
</code></pre>

<p>Usage:</p>

<pre><code>internal let sessionManager = SessionManager()

override init() {
    super.init()
    sessionManager.adapter = VMRequestAdapter()
    sessionManager.retrier = VMRetrier()
}
</code></pre>

<p><strong>UPDATE</strong>
There is a new commit in alamofire to provide a retry count option.<a href="https://github.com/Alamofire/Alamofire/issues/1672" rel="noreferrer">Alamofire Commit</a></p>