debugMinor
Error-handling #ifdefs for AFNetworking requests
Viewed 0 times
handlingerrorforifdefsrequestsafnetworking
Problem
I am using AFNetworking 1.4.3 to send and receive network messages in iOS. My application works slightly differently in DEBUG and RELEASE mode, so I need to use
-
-
Whole code:
```
@implementation ELHTTPClient
self = [super initWithBaseURL:url];
if (self) {
__weak ELHTTPClient *weakSelf = self;
[self setParameterEncoding:AFJSONParameterEncoding];
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:@"Accept" value:@"application/json"];
[self setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
[weakSelf.delegate httpClient:weakSelf connectionStateChanged:status];
}];
}
return self;
}
[self postPath:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([responseObject isResponseValid]) {
if (success) success(responseObject);
}
else {
#ifdef DEBUG
if (failure) failure([NSError fromDict:responseObject]);
#else
if (failure) failure([NSError networkError]);
#endif
}
} failure:^(AFHTTPRequestOperation operation, NSError error) {
// [self.delegate httpClient:self networkError:error];
// NSLog(@"Network error. Operation: %@, \n Error: %@", operation, error);
#ifdef DEBUG
if (failure) failure(error);
#else
if (failure) failure([NSError
#ifdef clauses. How can I simplify the following fragments without making these macros:-
#ifdef DEBUG
if (failure) failure([NSError fromDict:responseObject]);
#else
if (failure) failure([NSError networkError]);
#endif-
#ifdef DEBUG
if (failure) failure(error);
#else
if (failure) failure([NSError networkError]);
#endifWhole code:
```
@implementation ELHTTPClient
- (instancetype)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (self) {
__weak ELHTTPClient *weakSelf = self;
[self setParameterEncoding:AFJSONParameterEncoding];
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:@"Accept" value:@"application/json"];
[self setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
[weakSelf.delegate httpClient:weakSelf connectionStateChanged:status];
}];
}
return self;
}
- (void)myPostPath:(NSString )path parameters:(NSDictionary )parameters success:(void (^)(NSDictionary *))success failure:(ELErrorBlock)failure {
[self postPath:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([responseObject isResponseValid]) {
if (success) success(responseObject);
}
else {
#ifdef DEBUG
if (failure) failure([NSError fromDict:responseObject]);
#else
if (failure) failure([NSError networkError]);
#endif
}
} failure:^(AFHTTPRequestOperation operation, NSError error) {
// [self.delegate httpClient:self networkError:error];
// NSLog(@"Network error. Operation: %@, \n Error: %@", operation, error);
#ifdef DEBUG
if (failure) failure(error);
#else
if (failure) failure([NSError
Solution
I would put
ifdef DEBUG inside the failure method, so it's handled in one place only. (void) failure(id response, NSError error) {
#ifdef DEBUG
#else
#endif
}Code Snippets
(void) failure(id response, NSError error) {
#ifdef DEBUG
#else
#endif
}Context
StackExchange Code Review Q#68068, answer score: 2
Revisions (0)
No revisions yet.