我想使用Alamofire通过“通用”功能提出经过验证的网络请求。 如果我根本不使用RX,或者不调用validate()或anyCustomValidate(),则可以正常工作,但如果将其与Observable.create和validate()一起使用,则如下所示,它将不起作用。 而是这样做的:
1.makes tokenRefresh call
2.receives the new token
3.calls completion(true, 0.0) (as below)
但是什么也没发生...
这是我的通用函数:
func sendRequest<Response: Codable>(endpoint: URLRequestConvertible) -> Observable<Response> {
return Observable<Response>.create { observer in
let request = self.session.request(endpoint)
.validate()
.responseJSON { response in
print(response)
switch response.result {
case .success:
do {
let model = try JSONDecoder().decode(Response.self, from: response.data!)
observer.onNext(model)
} catch {
print("generic call function error: \(error)")
observer.onError(RequestError.failedParsingError(ErrTypes.somethingWrong.rawValue))
}
case .failure:
observer.onError(RequestError.failedParsingError(ErrTypes.somethingWrong.rawValue))
}
}
return Disposables.create {
request.cancel()
}
}.observeOn(MainScheduler.instance)
}
这是我用来获取特定回复的方式:
func getAddresses(clientId: Int) -> Observable<[AddressesResponse]> {
return sendRequest(endpoint: CustomerEndPoint.getAddresses(userID: clientId))
}
这就是我调用该函数的方式:
@objc func buttonPressed() {
getAddresses(clientId: 1111).subscribe(onNext: { addresses in
print(addresses)
}).disposed(by: disposeBag)
}
这是我的请求重试器:
extension AuthHandler: RequestRetrier {
func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
guard let request = request as? DataRequest, request.delegate.data != nil else { fatalError() }
guard let headerStatusCode = request.response?.statusCode else {
completion(false, 0.0)
return
}
if headerStatusCode == 401 || headerStatusCode == 403 {
refreshToken(success: {
completion(true, 0.0)
}, failure: { message in
print(message)
})
} else {
if let json = String(data: request.delegate.data!, encoding: String.Encoding.utf8),
let jsonObject = dictionaryWithJSONString(json) as? [String:Any],
let statusMessage = jsonObject["statusMessage"] as? [String: Any],
let status = statusMessage["status"] as? Int {
if status == 401 || status == 403 {
self.refreshToken(success: {
completion(true, 0.0)
}, failure: { message in
completion(false, 0.0)
print(message)
})
}
}
}
}
过一会儿,它会打印在控制台日志中:
2019-02-17 22:25:13.368846+0100 AlamofireValidator[76262:1527976] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C1.1:2][0x7fc5cf50e020] get output frames failed, state 8196
答案 0 :(得分:1)
像这样的错误几乎总是意味着您的完成块或观察者没有在其中一个路径中被调用。
在这种特定情况下,在我看来,您的最后几笔支票是否可疑。将以下两个打印语句放入其中,并在其上放置断点。我想您会发现其中之一被触发了。
{
if let json = String(data: request.delegate.data!, encoding: String.Encoding.utf8),
let jsonObject = dictionaryWithJSONString(json) as? [String:Any],
let statusMessage = jsonObject["statusMessage"] as? [String: Any],
let status = statusMessage["status"] as? Int {
if status == 401 || status == 403 {
self.refreshToken(success: {
completion(true, 0.0)
}, failure: { message in
completion(false, 0.0)
print(message)
})
}
else {
print("the problem is here")
}
}
else {
print("the problem is here.")
}