MTurner MTurner -4 years ago 258
iOS Question

AFNetworking - How to setup requests to be retried in the event of a timeout?

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


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

This is AFNetworking if you are unfamiliar

I was unable to find an equivalent api in AFNetworking, has anyone found a solution for retrying network requests in the event of timeout using AFNetworking?

Answer Source

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

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 = ...;
    } else {
        [self getPath:@"/path/to/file" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (success) {
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [self downloadFileRetryingNumberOfTimes:ntimes - 1 success:success failure:failure];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download