ios - Moving to a background NSURLSession from a foreground NSURLSession - Handling Tasks in Process -


i trying correctly handle in-process nsurlsessiontasks in event app enters background (e.g home button press). taking approach of copying in-process tasks across background queue (see code below). finding background tasks behaving erratically , not finishing. can spot might doing wrong / advise on best approach ?

- (void)appwillresignactive : (nsnotification *)notification {     uiapplication *app = [uiapplication sharedapplication];      // register expiring background task     __block uibackgroundtaskidentifier bgtaskid =     [app beginbackgroundtaskwithexpirationhandler:^{         bgtaskid = uibackgroundtaskinvalid;     }];      [self switchtobackground];     [app endbackgroundtask:bgtaskid];  }  - (void)appwillbecomeactive : (nsnotification *)notification {     [self switchtoforeground]; }  - (void)switchtobackground {     nslog(@"switch background line 217 network manager");     if ([state isequaltostring: kdownloadmanagerstateforeground]) {         [urlsession gettaskswithcompletionhandler:^(nsarray *datatasks, nsarray *uploadtasks, nsarray *downloadtasks) {             (nsurlsessiondownloadtask *downloadtask in downloadtasks) {                 [downloadtask cancelbyproducingresumedata:^(nsdata *resumedata) {                     nsurlsessiondownloadtask *downloadtask = [self.backgroundsession downloadtaskwithresumedata:resumedata];                     [downloadtask resume];                 }];             }         }];          state = kdownloadmanagerstatebackground;     } }  - (void)switchtoforeground {     if ([state isequaltostring: kdownloadmanagerstatebackground]) {         [backgroundsession gettaskswithcompletionhandler:^(nsarray *datatasks, nsarray *uploadtasks, nsarray *downloadtasks) {             (nsurlsessiondownloadtask *downloadtask in downloadtasks) {                 [downloadtask cancelbyproducingresumedata:^(nsdata *resumedata) {                     nsurlsessiondownloadtask *downloadtask = [self.urlsession downloadtaskwithresumedata:resumedata];                     [downloadtask resume];                 }];             }         }];          state = kdownloadmanagerstateforeground;     } } 

background sessions let perform uploads , downloads of content in background while app not running. can create background session configuration calling backgroundsessionconfiguration: method on nsurlsessionconfiguration class.

nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url]; nsurlsessionconfiguration *sessionconfig; float timeout = 5 * 60.0f;  bool ios8ornew = [[[uidevice currentdevice] systemversion] floatvalue] >= 8.0; if (ios8ornew) {    sessionconfig = [nsurlsessionconfiguration       backgroundsessionconfigurationwithidentifier:identifier];    request.timeoutinterval = timeout; } else {    sessionconfig = [nsurlsessionconfiguration        backgroundsessionconfiguration:identifier];   sessionconfig.timeoutintervalforrequest = timeout; }  sessionconfig.httpmaximumconnectionsperhost = 10; afurlsessionmanager *manager = [[afurlsessionmanager alloc] initwithsessionconfiguration:sessionconfig];  nsurlsessiondownloadtask *downloadtask = [manager downloadtaskwithrequest:request];   [manager setdidfinisheventsforbackgroundurlsessionblock:^(nsurlsession * _nonnull session) { appdelegate *appdelegate = (appdelegate *)[[uiapplication sharedapplication] delegate]; if (appdelegate.backgroundsessioncompletionhandler) {     void (^completionhandler)() = appdelegate.backgroundsessioncompletionhandler;     appdelegate.backgroundsessioncompletionhandler = nil;     completionhandler(); } nslog(@"all tasks finished"); }]; 

for more information please see answer : how keep downloading new images in background if user force quits app in ios objective c?


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -