ios - What's the use of reachability in AFNetworking? -


when in middle of download connection lost or there no connection initially, completionhandler fired error , have no chance resume after connection restored. proper way handle resumable downloading afnetworking/reachability? have create task because 1 expired due network failure or there way revive it?

nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration]; afurlsessionmanager *man = [[afurlsessionmanager alloc] initwithsessionconfiguration:configuration];  nsurl *url = [nsurl urlwithstring:@"http://my_server.com/video/2.mp4"]; nsurlrequest *request = [nsurlrequest requestwithurl:url];  nsurlsessiondownloadtask *downloadtask = [man downloadtaskwithrequest:request progress:nil destination:^nsurl *(nsurl *targetpath, nsurlresponse *response) {     nsurl *documentsdirectoryurl = [[nsfilemanager defaultmanager] urlfordirectory:nsdocumentdirectory indomain:nsuserdomainmask appropriateforurl:nil create:no error:nil];     return [documentsdirectoryurl urlbyappendingpathcomponent:[response suggestedfilename]]; } completionhandler:^(nsurlresponse *response, nsurl *filepath, nserror *error) {     nslog(@"file downloaded to: %@, error: %@", filepath, error); }];  [man.reachabilitymanager startmonitoring]; [man.reachabilitymanager setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) {     switch (status) {         case afnetworkreachabilitystatusreachableviawwan:         case afnetworkreachabilitystatusreachableviawifi:             [downloadtask resume];             break;         case afnetworkreachabilitystatusnotreachable:         default:             [downloadtask suspend];             break;     } }]; 

what proper way handle resumable downloading afnetworking/reachability? have create task because 1 expired due network failure or there way revive it?

what asking how resume download after connection failure. start nsurlsessiondownloadtask, , during download connection fails appropriate nserror describing failure. want retry connection, reusing downloaded data, when network interface reachability changes.

nsurlsessiondownloadtask can reuse downloaded data if application grabs resume data , later passes task retries connection. documented here , here.

in case of network failure, tthe nserror returned have userinfo dictionary populated nsurlsessiondownloadtaskresumedata key. have data need hold on to. when application attempts download data again, download task should created using either downloadtaskwithresumedata: or downloadtaskwithresumedata:completionhandler:, passing in resume data.

in case, after network failure returns nserror nsurlerrordomain, appropriate error code, , userinfo dictionary populated nsurlsessiondownloadtaskresumedata key, hold on nsdata value nsurlsessiondownloadtaskresumedata key , start monitoring reachability changes. if network interface comes back, in reachability change notification handler create new download task using downloadtaskwithresumedata: or downloadtaskwithresumedata:completionhandler: , pass nsdata retreived nserror's userinfo dictionary.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -