ios - NSURLSessionDownloadTask and retrying -


i using following code download file web. how structure code (using blocks) such that, if fails, retried max of retrycount. here code using download file.

    nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration];     afurlsessionmanager *manager = [[afurlsessionmanager alloc] initwithsessionconfiguration:configuration];      nsurl *myurl = [nsurl urlwithstring:@"urlstring"];      nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:myurl];      [request setvalue:token forhttpheaderfield:@"authorization"];      nsurlsessiondownloadtask *downloadtask = [manager downloadtaskwithrequest:request progress:nil destination:^nsurl *(nsurl *targetpath, nsurlresponse *response) {         nsurl *directoryurl = [nsurl fileurlwithpath:nstemporarydirectory()];         return [directoryurl urlbyappendingpathcomponent:filename];         } completionhandler:^(nsurlresponse *response, nsurl *filepath, nserror *error) {               //check here if error retry using block ?                 if (!error)                  {                      //do file                  }                  else                   {                        //retry download ??                   }       }   }];    [downloadtask resume]; 

you create retrycount instance variable , set many times want retry network call. put networking code in method custom completion handler. completion handler block takes bool parameter , returns void. then, in networking code, create local bool variable , set yes or no based on whether network call succeeded or failed. you'll run completion handler argument within networking code. block (completion handler) send parameter method can "see" whether local variable in method set yes or no. if yes, network call succeeded, nothing. if no, network call failed, check retrycount--if it's > 0, call networking method again , decrement retrycount.

update

this should give rough idea...

typedef void (^customcompletionhandler)(bool); // create customcompletionhandler type  - (void)getdataandcallcompletionhandler:(customcompletionhandler *)completionhandler {     nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration];     afurlsessionmanager *manager = [[afurlsessionmanager alloc] initwithsessionconfiguration:configuration];      nsurl *myurl = [nsurl urlwithstring:@"urlstring"];      nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:myurl];      [request setvalue:token forhttpheaderfield:@"authorization"];      nsurlsessiondownloadtask *downloadtask = [manager downloadtaskwithrequest:request progress:nil destination:^nsurl *(nsurl *targetpath, nsurlresponse *response) {          bool success = no; // create local variable keep track of whether network call succeeded or failed          nsurl *directoryurl = [nsurl fileurlwithpath:nstemporarydirectory()];         return [directoryurl urlbyappendingpathcomponent:filename];         } completionhandler:^(nsurlresponse *response, nsurl *filepath, nserror *error) {               //check here if error retry using block ?                 if (!error)                  {                      success = yes;                      //do file                  }                  else                   {                      success = no;                      //retry download ??                   }       }   // main queue , call completionhandler , pass success:   // completionhandler(success)   }]; [downloadtask resume]; } 

you call method doing like...

[self getdataandcallcompletionhandler:^(bool success) {     // if success == true {       //     whatever need     // } else {     //     check retrycount     //     if need to, call getdataandcallcompletionhandler: again     // } }]; 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -