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

前端 未结 4 765
攒了一身酷
攒了一身酷 2020-12-12 16:01

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 req

4条回答
  •  独厮守ぢ
    2020-12-12 16:10

    Based on your answers, you could do something even more generic (and tricky) by using a block taking as parameter a block :

    typedef void (^CallbackBlock)(NSError* error, NSObject* response);
    - (void) performBlock:(void (^)(CallbackBlock callback)) blockToExecute retryingNumberOfTimes:(NSUInteger)ntimes onCompletion:(void (^)(NSError* error, NSObject* response)) onCompletion {
        blockToExecute(^(NSError* error, NSObject* response){
            if (error == nil) {
                onCompletion(nil, response);
            } else {
                if (ntimes <= 0) {
                    if (onCompletion) {
                        onCompletion(error, nil);
                    }
                } else {
                    [self performBlock:blockToExecute retryingNumberOfTimes:(ntimes - 1) onCompletion:onCompletion];
                }
            };
        });
    }
    

    Then surround your asynchronous HTTP requests like the following :

    [self performBlock:^(CallbackBlock callback) {
    
            [...]
            AFHTTPRequestOperationManager *manager = [WSManager getHTTPRequestOperationManager];
            [manager POST:base parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
                dispatch_async(dispatch_get_main_queue(), ^(void){
                        if (callback) {
                            callback(nil, responseObject);
                        }
                    });
    
                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                    if (callback) {
                        NSError* errorCode = [[NSError alloc] initWithDomain:AppErrorDomain code:[operation.response statusCode] userInfo:@{ NSLocalizedDescriptionKey :error.localizedDescription}];
                        callback(errorCode, nil);
                    }
                }];
    
        } retryingNumberOfTimes:5 onCompletion:^(NSError *error,  NSObject* response) {
            //everything done
        }];
    

    This way the retries wait for the HTTP request to finish and you don't have to implement the retry loop in each request methods.

提交回复
热议问题