Note that there are some explanatory texts on larger screens.

plurals
  1. POMethod calling itself with identical params
    text
    copied!<p>I have several "proxy" classes, which inherit from a "base proxy". These classes connect to my server and pass data to their delegates. In event of 0 status code, I want to handle these different requests in the same way.</p> <p>For 0 status codes, I want to retry the method in 5 seconds, hoping the user's internet connection has improved.</p> <h1>SomethingProxy.m</h1> <pre><code>- (void)fetchSomething { NSString *fullPath = [NSString stringWithFormat:@"%@/route/index.json",MY_BASE_URL]; NSURL *url = [NSURL URLWithString:fullPath]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSDictionary *d = (NSDictionary *)responseObject; [self.delegate fetchedPolicy:d[@"keypath"]]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { [self handleOperationFailed:operation action:^{ [self fetchSomething]; }]; }]; NSOperationQueue *q = [[NSOperationQueue alloc] init]; [q addOperation:operation]; } </code></pre> <h1>MyBaseProxy.m</h1> <pre><code>- (bool)shouldRetryOperation:(AFHTTPRequestOperation *)o { return self.retries &lt; [self maxRetries]; } - (void)handleOperationFailed:(AFHTTPRequestOperation *)o action:(ActionBlock)block { NSInteger statusCode = o.response.statusCode; if (statusCode == 0) { if ([self shouldRetryOperation:o]) { double delayInSeconds = 5.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ self.retries++; block(); }); } else { self.retries = 0; [SVProgressHUD showErrorWithStatus:@"Please check your internet connection and try again"]; return; } } self.retries = 0; </code></pre> <p>What's a better way to handle the request failure? Should I subclass AFHTTPRequestOperation?</p> <p>EDIT: Removed confusing text. When I meant "same way", I meant per request Eg. handle all 500s the same, handle all 403s the same. I'm specifically asking for handling status code 0 - no internet connection.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload