我试图让请求更像Alamofire页面中的示例,但我收到此错误:
这是Request Retrier代码:
public typealias RequestRetryCompletion = (_ shouldRetry: Bool, _ timeDelay: TimeInterval) -> Void
public protocol RequestRetrier {
func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion)}
class RequestRetryHandler: RequestRetrier {
public func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 503 {
completion(true, 10.0) // retry after 1 second
} else {
completion(false, 0.0) // don't retry
}
}}
这就是错误发生的地方:
let manager = Alamofire.SessionManager.default
manager.retrier = RequestRetryHandler() as? RequestRetrier
答案 0 :(得分:4)
您不应该重新声明协议或RequestRetryCompletion
。这已经在Alamofire为你完成了。您需要做的就是:
import Alamofire
import Foundation
class RequestRetryHandler: RequestRetrier {
func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 503 {
completion(true, 10.0) // retry after 1 second
} else {
completion(false, 0.0) // don't retry
}
}
}
let manager = Alamofire.SessionManager.default
manager.retrier = RequestRetryHandler()
祝你好运!