Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions AFHTTPRequestOperationManager+AutoRetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedMethodInspection"

#define INCREASE_TIMEOUT_RATIO 1.5

typedef int (^RetryDelayCalcBlock)(int, int, int); // int totalRetriesAllowed, int retriesRemaining, int delayBetweenIntervalsModifier

@interface AFHTTPRequestOperationManager (AutoRetry)

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSMutableURLRequest *)request
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
autoRetryOf:(int)retriesRemaining
Expand Down Expand Up @@ -115,4 +117,4 @@ typedef int (^RetryDelayCalcBlock)(int, int, int); // int totalRetriesAllowed, i

@end

#pragma clang diagnostic pop
#pragma clang diagnostic pop
14 changes: 11 additions & 3 deletions AFHTTPRequestOperationManager+AutoRetry.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ - (id)operationsDict {
return self.__operationsDict;
}

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSMutableURLRequest *)request
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
autoRetryOf:(int)retriesRemaining retryInterval:(int)intervalInSeconds {
Expand All @@ -59,6 +59,14 @@ - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)requ
[self.operationQueue addOperation:retryOperation];
};
RetryDelayCalcBlock delayCalc = self.retryDelayCalcBlock;

if ([request isKindOfClass:[NSMutableURLRequest class]]) {
request.timeoutInterval=request.timeoutInterval*INCREASE_TIMEOUT_RATIO;
NSLog(@"AutoRetry: increasing time to %f seconds...", request.timeoutInterval);
} else {
NSLog(@"AutoRetry: not increasing timeout");
}

int intervalToWait = delayCalc(originalRetryCount, retriesRemainingCount, intervalInSeconds);
if (intervalToWait > 0) {
NSLog(@"AutoRetry: Delaying retry for %d seconds...", intervalToWait);
Expand All @@ -70,7 +78,7 @@ - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)requ
addRetryOperation();
}
} else {
NSLog(@"AutoRetry: Request failed %d times: %@", originalRetryCount, error.localizedDescription);
NSLog(@"AutoRetry: Request %@ failed %d times: %@",request, originalRetryCount, error.localizedDescription);
NSLog(@"AutoRetry: No more retries allowed! executing supplied failure block...");
failure(operation, error);
NSLog(@"AutoRetry: done.");
Expand Down Expand Up @@ -227,4 +235,4 @@ - (AFHTTPRequestOperation *)DELETE:(NSString *)URLString

@end

#pragma clang diagnostic pop
#pragma clang diagnostic pop