Is there a way to do Alamofire requests with retries

后端 未结 4 1998
离开以前
离开以前 2020-12-15 09:09

I have a lot of places in the code where Alamofire request/response are handled.

Each of this requests may fail because of some intermittent problem (the most commo

4条回答
  •  执念已碎
    2020-12-15 09:33

    I've had the same problem, and I got the requests to be retried using the RequestRetrier, should method and request.retryCount. Something like it:

    // MARK: - RequestRetry
    
    public func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
        lock.lock() ; defer { lock.unlock() }
    
    
        if let response = request.task?.response as? HTTPURLResponse{
            if response.statusCode == 401 {
                requestsToRetry.append(completion)
    
                getToken { (expires, _) in
                   _ = SessionCountdownToken.sharedInstance.startCount(expirationTime: expires)
                }
            } else {
    
                if request.retryCount == 3 { completion(false, 0.0 ); return}
                completion(true, 1.0)
                return
            }
        } else {
            completion(false, 0.0)
        }
    }
    

提交回复
热议问题