AFNetworking:重试操作时访问完成处理程序 [英] AFNetworking: Access to completion handlers when retrying operation
问题描述
给出一些上下文:我正在尝试为身份验证错误(使用令牌身份验证,而不是基本身份)实现全局错误处理程序,它应该尝试重新进行身份验证,然后重复原始失败的请求(请参阅我之前的问题: AFNetworking:全局处理错误并重复请求)
To give some context: I'm trying to implement a global error handler for authentication errors (using token authentication, not basic), which should try to re-authenticate and then repeat the original failed request (see my previous question: AFNetworking: Handle error globally and repeat request)
当前的方法是注册 AFNetworkingOperationDidFinishNotification
的观察者,该观察者进行重新认证并且(如果auth成功)重复原始请求:
The current approach is to register an observer for the AFNetworkingOperationDidFinishNotification
which does the re-authentication and (if auth succeeded) repeats the original request:
- (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 original success/failure blocks here?
[self enqueueHTTPRequestOperation:newOperation];
}
failure:^(NSError *error) {
// PROBLEM 2: How to invoke failure block of original operation?
}
];
}
}
然而,我偶然发现了一些关于完成块的问题请求操作:
However, I stumbled upon some issues regarding completion blocks of request operations:
-
重复原始请求时,我显然希望执行完成块。但是,
AFHTTPRequestOperation
不会保留对传递的成功和失败块的引用(请参阅setCompletionBlockWithSuccess:failure:
)并复制NSOperation
的completionBlock
可能不是一个好主意,因为AFURLConnectionOperation的文档
州:
When repeating the original request, I obviously want its completion blocks to be executed. However,
AFHTTPRequestOperation
does not retain references to the passed success and failure blocks (seesetCompletionBlockWithSuccess:failure:
) and copyingNSOperation
'scompletionBlock
is probably not a good idea, as the documentation forAFURLConnectionOperation
states:
操作副本不包括
completionBlock
。completionBlock
经常会强烈捕获对self
的引用,这可能会令人惊讶地指向原始复制时的操作。
Operation copies do not include
completionBlock
.completionBlock
often strongly captures a reference toself
, which, perhaps surprisingly, would otherwise point to the original operation when copied.
如果重新验证失败,我想调用原始请求的失败块。所以,再一次,我需要直接访问它。
In case the re-authentication fails, I want to call the original request's failure block. So, again, I'd need direct access to this.
我在这里遗漏了什么吗?任何替代方法的想法?我应该提交功能请求吗?
Am I missing something here? Any ideas for alternative approaches? Should I file a feature request?
推荐答案
我在 Art.sy 的投资组合应用。我最终的结论是创建了一个NSOperationQueue子类,它具有在失败后创建各种AFNetworking HTTP操作的副本的功能(并且在放弃之前每个URL最多执行三次。)
I've come up with this problem in Art.sy's portfolio app. My eventual conclusion was to create a NSOperationQueue subclass which had functions to create copies of various AFNetworking HTTP Operations once they failed (and to do this up to three times per URL before giving up.)
这篇关于AFNetworking:重试操作时访问完成处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!