objective c - Method calling itself with identical params -


i have several "proxy" classes, inherit "base proxy". these classes connect server , pass data delegates. in event of 0 status code, want handle these different requests in same way.

for 0 status codes, want retry method in 5 seconds, hoping user's internet connection has improved.

somethingproxy.m

- (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]; } 

mybaseproxy.m

- (bool)shouldretryoperation:(afhttprequestoperation *)o {     return self.retries < [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 internet connection , try again"];         return;     } }  self.retries = 0; 

what's better way handle request failure? should subclass afhttprequestoperation?

edit: removed confusing text. when meant "same way", meant per request eg. handle 500s same, handle 403s same. i'm asking handling status code 0 - no internet connection.

the key situation should concerned with, imho, when network, itself, not available device. in case, you'd use reachability have notification posted app when network becomes available. no point in repeatedly retrying when (a) know network unavailable; , (b) can notified when network becomes available again.

if you're concerned server-specific issues, careful send request in 5 seconds. let's server overwhelmed , cannot respond client requests. having large numbers of clients proceeding retry every 5 seconds might not improve situation. sort of depends upon why server not responding. want make sure attempts connect again never make situation worse.

(as aside, description 403 says, "the server understood request, refusing fulfill it. authorization not , request should not repeated.")

if you're going employ retry process, i'd specify reasonable maximum number of retries (which, judging revised answer, doing). , perhaps you'd want when app returns foreground, reinitiate "retry 3 times, once every 15 seconds" logic (or whatever settle upon). scenario i'm thinking "user started app, failed after exceeding max retries, , hit home button on device, , few minutes/hours later, tap on app again (which might still running)."


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -