我想通过“通用”功能使用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
像这样的错误几乎总是意味着你的完成块或观察者没有在其中一个路径中被调用。
在这个特定的情况下,它在我看来你的最后几个if检查是可疑的。将下面的两个打印语句放入其中并在其上添加断点。我想你会发现其中一个被触发了。
{
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.")
}