AFNetworking:重试操作时访问完成处理程序

Dan*_*ser 7 networking ios afnetworking

为了让一些背景:我试图实现对身份验证错误(使用令牌认证,而不是基本的),一个全球性的错误处理程序,它应该尝试重新进行身份验证,然后重复原始失败的请求(参见我刚才的问题: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 original success/failure blocks here?

                [self enqueueHTTPRequestOperation:newOperation];
            }
            failure:^(NSError *error) {
                // PROBLEM 2: How to invoke failure block of original operation?
            }
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我偶然发现了一些有关请求操作完成块的问题:

  • 重复原始请求时,我显然希望执行完成块.然而,AFHTTPRequestOperation不保留(见所传递的成功和失败块引用setCompletionBlockWithSuccess:failure:)和复制NSOperationcompletionBlock可能不是一个好主意,因为对于文档AFURLConnectionOperation的状态:

    操作副本不包括completionBlock.completionBlock通常强烈地捕获一个引用self,这可能会令人惊讶,否则将指向复制时的原始操作.

  • 如果重新验证失败,我想调用原始请求的失败块.所以,再一次,我需要直接访问它.

我在这里错过了什么吗?任何替代方法的想法?我应该提交功能请求吗?

ort*_*rta 1

我在Art.sy的作品集应用程序中遇到了这个问题。我的最终结论是创建一个 NSOperationQueue 子类,它具有在失败后创建各种 AFNetworking HTTP 操作副本的功能(并且在放弃之前为每个 URL 执行最多 3 次)。