我的问题与此问题非常相似,但对于Alamofire:AFNetworking:处理全局错误并重复请求
如何能够捕获全局错误(通常是401)并在其他请求发出之前处理它(如果不管理,最终会失败)?
我正在考虑链接自定义响应处理程序,但在应用程序的每个请求上执行此操作都很愚蠢.
也许是子类化,但是我应该将哪个类子类来处理呢?
为了让一些背景:我试图实现对身份验证错误(使用令牌认证,而不是基本的),一个全球性的错误处理程序,它应该尝试重新进行身份验证,然后重复原始失败的请求(参见我刚才的问题:AFNetworking:手柄全局错误并重复请求)
当前的方法是注册一个观察者,该观察者进行AFNetworkingOperationDidFinishNotification重新认证,并且(如果auth成功)重复原始请求:
- (void)operationDidFinish:(NSNotification *)notification
{
AFHTTPRequestOperation *operation = (AFHTTPRequestOperation *)[notification object];
if(![operation isKindOfClass:[AFHTTPRequestOperation class]]) {
return;
}
if(403 == [operation.response statusCode]) {
// try to re-authenticate and repeat the original request
[[UserManager sharedUserManager] authenticateWithCredentials...
success:^{
// repeat original request
// AFHTTPRequestOperation *newOperation = [operation copy]; // copies too much stuff, eg. response (although the docs suggest otherwise)
AFHTTPRequestOperation *newOperation = [[AFHTTPRequestOperation alloc] initWithRequest:operation.request];
// PROBLEM 1: newOperation has no completion blocks. How to use the …Run Code Online (Sandbox Code Playgroud)