Alamofire 5.0.0-rc.3 RequestInterceptor Adapt方法未调用Alamofire,尽管在响应中出现任何错误时都会调用重试

时间:2019-11-14 08:59:41

标签: ios swift alamofire

Alamofire 5.0.0-rc.3虽然在响应中有任何错误时都会调用重试,但Alamofire不会调用RequestInterceptor Adapt方法。

方法:

    func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (AFResult<URLRequest>) -> Void) {
}
class Interceptor: RequestInterceptor {

      func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (AFResult<URLRequest>) -> Void) {
           print("ADAPT :=")
           completion(.success(urlRequest))
       }

       func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
           print("RETRY :=")
           completion(.doNotRetry)

       }
}

属于不同类的网络请求方法:

    public func request<T: Codable> (_ urlConvertible: URLRequestConvertible) -> Observable<T> {
        return Observable<T>.create { observer in

            // 1
            print("Url := \(urlConvertible.urlRequest!.url!)")

            // 2
            let request = AF.request(urlConvertible, interceptor: Interceptor()).responseDecodable { (response: AFDataResponse<T>) in

                if let data = response.data{
                    print("Response := \(String(decoding: data, as: UTF8.self))")
                }
                else{
                    print("data is nil")
                }

                switch response.result {
                case .success(let value):
                    print("value :-> \(value)")
                    observer.onNext(value)
                    observer.onCompleted()
                case .failure(let error):

                    observer.onError(error)
                }
            }

            //Finally, we return a disposable to stop the request
            return Disposables.create {
                request.cancel()
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

查看此处:

https://github.com/Alamofire/Alamofire/issues/2998

由于存在歧义而未调用该函数...

在拦截器中:

添加此内容:

typealias AdapterResult = Swift.Result<URLRequest, Error>

并在“ adapt”方法中,将@escaping参数替换为:

@escaping (RetryResult)

应该可以。