Last active
August 29, 2015 14:06
-
-
Save sergii-frost/633ca6c079dfe5541171 to your computer and use it in GitHub Desktop.
AFNetworkingClient + Retry logics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AFNetworkingClient+Retry | |
// | |
// Created by Sergii Nezdolii on 2014-09-23. | |
// | |
// | |
@interface AFNetworkingClient : NSObject | |
@property (strong, readonly) AFHTTPRequestOperationManager *manager; | |
+ (AFNetworkingClient *) sharedInstance; | |
- (void)getSomeDataWithSuccess:(void (^)(id responseObject))success | |
failure:(void (^)(NSError *error))failure | |
numberOfRetries:(NSUInteger)numberOfRetries; | |
@end | |
@implementation AFNetworkingClient | |
static AFNetworkingClient *instance; | |
- (instancetype)init { | |
if (self = [super init]) { | |
_manager = [AFHTTPRequestOperationManager manager]; | |
_manager.requestSerializer = [AFJSONRequestSerializer serializer]; | |
_manager.responseSerializer = [AFJSONResponseSerializer serializer]; | |
} | |
return self; | |
} | |
+ (AFNetworkingClient *)sharedInstance { | |
static dispatch_once_t _singletonPredicate; | |
dispatch_once(&_singletonPredicate, ^{ | |
instance = [AFNetworkingClient new]; | |
}); | |
return instance; | |
} | |
- (void)getSomeDataWithSuccess:(void (^)(id responseObject))success | |
failure:(void (^)(NSError *error))failure | |
numberOfRetries:(NSUInteger)numberOfRetries { | |
NSString *someUrl = @"https://api.github.com/users/snezdoliy/repos" | |
[self GET:someUrl parameters:nil | |
success:^(AFHTTPRequestOperation *operation, id responseObject) { | |
if(success) { | |
success(responseObject); | |
} | |
} | |
failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
if(failure) { | |
failure(error); | |
} | |
} numberOfRetries:numberOfRetries prevFailureOperation:nil prevFailureError:nil]; | |
} | |
#pragma mark - Helper methods to handle Retry properly | |
- (void)GET:(NSString *)url parameters:(id)parameters | |
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success | |
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure | |
numberOfRetries:(NSUInteger)numberOfRetries | |
prevFailureOperation:(AFHTTPRequestOperation *)prevFailureOperation | |
prevFailureError:(NSError *)prevFailureError { | |
if(numberOfRetries <= 0) { | |
if (failure) { | |
failure(prevFailureOperation, prevFailureError); | |
} | |
} else { | |
[_manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { | |
if (success) { | |
success(operation, responseObject); | |
} | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
[self GET:url parameters:parameters | |
success:success | |
failure:failure | |
numberOfRetries:numberOfRetries-1 | |
prevFailureOperation:operation | |
prevFailureError:error]; | |
}]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment