Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make failure block optional #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions AFNetworking+RetryPolicy/AFHTTPSessionManager+RetryPolicy.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,23 @@ - (BOOL)isErrorFatal:(NSError *)error {
return NO;
}

- (NSURLSessionDataTask *)requestUrlWithRetryRemaining:(NSInteger)retryRemaining maxRetry:(NSInteger)maxRetry retryInterval:(NSTimeInterval)retryInterval progressive:(bool)progressive fatalStatusCodes:(NSArray<NSNumber *> *)fatalStatusCodes originalRequestCreator:(NSURLSessionDataTask *(^)(void (^)(NSURLSessionDataTask *, NSError *)))taskCreator originalFailure:(void(^)(NSURLSessionDataTask *task, NSError *))failure {
- (NSURLSessionDataTask *)requestUrlWithRetryRemaining:(NSInteger)retryRemaining maxRetry:(NSInteger)maxRetry retryInterval:(NSTimeInterval)retryInterval progressive:(bool)progressive fatalStatusCodes:(NSArray<NSNumber *> *)fatalStatusCodes originalRequestCreator:(NSURLSessionDataTask *(^)(void (^)(NSURLSessionDataTask *, NSError *)))taskCreator originalFailure:(nullable void(^)(NSURLSessionDataTask *task, NSError *))failure {
void(^retryBlock)(NSURLSessionDataTask *, NSError *) = ^(NSURLSessionDataTask *task, NSError *error) {
if ([self isErrorFatal:error]) {
[self logMessage:@"Request failed with fatal error: %@ - Will not try again!", error.localizedDescription];
failure(task, error);
if (failure) {
failure(task, error);
}
return;
}

NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
for (NSNumber *fatalStatusCode in fatalStatusCodes) {
if (response.statusCode == fatalStatusCode.integerValue) {
[self logMessage:@"Request failed with fatal error: %@ - Will not try again!", error.localizedDescription];
failure(task, error);
if (failure) {
failure(task, error);
}
return;
}
}
Expand Down Expand Up @@ -179,7 +183,9 @@ - (NSURLSessionDataTask *)requestUrlWithRetryRemaining:(NSInteger)retryRemaining

} else {
[self logMessage:@"No more attempts left! Will execute the failure block."];
failure(task, error);
if (failure) {
failure(task, error);
}
}
};
NSURLSessionDataTask *task = taskCreator(retryBlock);
Expand Down