简体   繁体   中英

Alamofire 5.0.0-rc.3 RequestInterceptor Adapt method not being called of Alamofire although retry gets called when there is any error in response

Alamofire 5.0.0-rc.3 RequestInterceptor Adapt method not being called of Alamofire although retry gets called when there is any error in response.

Method:

    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)

       }
}

Network Request method which is in different class:

    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()
            }
        }

    }

See here:

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

The function is not called because there is an ambiguity...

In the Interceptor:

Add this:

typealias AdapterResult = Swift.Result<URLRequest, Error>

And replace in the "adapt" method, the @escaping parameter by this:

@escaping (RetryResult)

It should works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM