我正在使用Alamofire的SessionManager向我的API服务发出请求。

有没有办法限制Alamofire中的重试次数?

我正在使用新的RequestAdapter和RequestTrier协议(protocol)在Alamofire v4中提供重试机制。

我想将重试次数限制为5次。如果5次重试后不应执行新的请求。

class VMRetrier: RequestAdapter, RequestRetrier {

public func adapt(_ urlRequest: URLRequest) throws -> 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)
    }
} }

用法:
internal let sessionManager = SessionManager()

override init() {
    super.init()
    sessionManager.adapter = VMRequestAdapter()
    sessionManager.retrier = VMRetrier()
}

更新
alamofire中有一个新提交,可提供重试计数选项。 Alamofire Commit

最佳答案

我已经使用Alamofire的RequestRetrier协议(protocol)解决了这个问题:

class Retrier: RequestRetrier {

    var defaultRetryCount = 4
    private  var requestsAndRetryCounts: [(Request, Int)] = []
    private  var lock = NSLock()

    private func index(request: Request) -> Int? {
        return requestsAndRetryCounts.index(where: { $0.0 === request })
    }

    func addRetryInfo(request: Request, retryCount: Int? = nil) {
        lock.lock() ; defer { lock.unlock() }
        guard index(request: request) == nil else { print("ERROR addRetryInfo called for already tracked request"); return }

        requestsAndRetryCounts.append((request, retryCount ?? defaultRetryCount))
    }

    func deleteRetryInfo(request: Request) {
        lock.lock() ; defer { lock.unlock() }
        guard let index = index(request: request) else { print("ERROR deleteRetryInfo called for not tracked request"); return }

        requestsAndRetryCounts.remove(at: index)
    }

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

        lock.lock() ; defer { lock.unlock() }

        guard let index = index(request: request) else { completion(false, 0); return }
        let (request, retryCount) = requestsAndRetryCounts[index]

        if retryCount == 0 {
            completion(false, 0)
        } else {
            requestsAndRetryCounts[index] = (request, retryCount - 1)
            completion(true, 0.5)
        }
    }
}

应该以这种方式使用:
let manager = SessionManager()
let retrier = Retrier()
// ...
manager.retrier = retrier

然后
let request = manager.request(urlString, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers)

retrier.addRetryInfo(request: request)

request.response { r in
    self.retrier.deleteRetryInfo(request: request)
}.responseApiJSON { (r: DataResponse<JSONObject>) in
    // You code here
}

10-08 05:25
查看更多