AFNetworking - 如何设置在超时时重试的请求? [英] AFNetworking - How to setup requests to be retried in the event of a timeout?
问题描述
我最近从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?
如果您不熟悉,这是AFNetworking
https://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屋!