AFNetworking - 如何在超时的情况下设置要重试的请求?

MTu*_*ner 23 iphone asihttprequest ipad ios afnetworking

我最近从ASIHTTPRequest迁移到AFNetworking,这非常棒.但是,我连接的服务器有一些问题,有时会导致我的请求超时.使用ASIHTTPRequest时,可以使用以下选择器在超时时为请求设置重试计数

-setNumberOfTimesToRetryOnTimeout:
Run Code Online (Sandbox Code Playgroud)

这篇文章可以进一步引用,可以重试ASIHTTPRequest吗?

如果你不熟悉这就是AFNetworking https://github.com/AFNetworking/AFNetworking#readme

我无法在AFNetworking中找到等效的api,是否有人找到了在使用AFNetworking超时的情况下重试网络请求的解决方案?

MTu*_*ner 58

AFNetworking的Matt Thompson开发人员非常友好地为我解答了这个问题.下面是解释解决方案的github链接.

https://github.com/AFNetworking/AFNetworking/issues/393

基本上,AFNetworking不支持此功能.由开发人员根据具体情况实施如下所示(取自Matt Thompson在github上的回答)

- (void)downloadFileRetryingNumberOfTimes:(NSUInteger)ntimes 
                              success:(void (^)(id responseObject))success 
                              failure:(void (^)(NSError *error))failure
{
    if (ntimes <= 0) {
        if (failure) {
            NSError *error = ...;
            failure(error);
        }
    } else {
        [self getPath:@"/path/to/file" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (success) {
                success(...);
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [self downloadFileRetryingNumberOfTimes:ntimes - 1 success:success failure:failure];
        }];
    }
}
Run Code Online (Sandbox Code Playgroud)