AFNetworking - 如何设置在超时时重试的请求? [英] AFNetworking - How to setup requests to be retried in the event of a timeout?

查看:129
本文介绍了AFNetworking - 如何设置在超时时重试的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have recently migrated from ASIHTTPRequest to AFNetworking, which has been great. However, the server that I am connecting with has some issues and sometimes causes my requests to timeout. When using ASIHTTPRequest it was possible to setup a retry count on a request in the event of a timeout using the following selector

-setNumberOfTimesToRetryOnTimeout:

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

This can be further referenced in this post, Can an ASIHTTPRequest be retried?

如果您不熟悉,这是 AFNetworkinghttps://github.com/AFNetworking/AFNetworking#readme

This is AFNetworking if you are unfamiliar https://github.com/AFNetworking/AFNetworking#readme

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

推荐答案

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

Matt Thompson developer of AFNetworking was kind enough to answer this for me. Below is the github link explaining the solution.

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

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

Basically, AFNetworking doesn't support this functionality. It is left to the developer to implement on a case by case basis as shown below (taken from Matt Thompson's answer on 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];
        }];
    }
}

这篇关于AFNetworking - 如何设置在超时时重试的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆