iOS background task using NSURLSessionDataTask -


i have flight search feature in application taking long data (more 25 seconds). if application goes background or went sleep mode, internet connection disconnected.

i have written below logic using apple example make api request keep going though if app goes background it's not working.

self.session = [self backgroundsession]; self.mutabledata = [nsmutabledata data];  nsurl *downloadurl = [nsurl urlwithstring:@"http://jsonplaceholder.typicode.com/photos"]; nsurlrequest *request = [nsurlrequest requestwithurl:downloadurl]; self.datatask = [self.session datataskwithrequest:request]; [self.datatask resume];  - (nsurlsession *)backgroundsession {     static nsurlsession *session = nil;     static dispatch_once_t oncetoken;     dispatch_once(&oncetoken, ^{         nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration backgroundsessionconfigurationwithidentifier:@"com.example.apple-samplecode.simplebackgroundtransfer.backgroundsession"];         session = [nsurlsession sessionwithconfiguration:configuration delegate:self delegatequeue:nil];     });     return session; } 

below delegate methods

- (void)urlsession:(nsurlsession *)session datatask:(nsurlsessiondatatask *)datatask didreceiveresponse:(nsurlresponse *)response completionhandler:(void (^)(nsurlsessionresponsedisposition disposition))completionhandler {  nslog(@"response: %@", response.debugdescription);  nsurlsessionresponsedisposition disposition = nsurlsessionresponseallow;     if (completionhandler) {         completionhandler(disposition);     } }  - (void)urlsession:(nsurlsession *)session datatask:(nsurlsessiondatatask *)datatask     didreceivedata:(nsdata *)data {     [self.mutabledata appenddata:data]; }   - (void)urlsession:(nsurlsession *)session task:(nsurlsessiontask *)task didcompletewitherror:(nserror *)error {     blog();      if (error == nil)     {         nsdata *data = nil;         if (self.mutabledata) {             data = [self.mutabledata copy];             self.mutabledata = nil;         }          nserror* error;         nsarray* json = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:&error];         if (!json) {             nslog(@"error parsing json: %@", error);         } else {             nslog(@"data: %@", json);         }     }     else     {         nslog(@"task: %@ completed error: %@", task, [error localizeddescription]);     }      double progress = (double)task.countofbytesreceived / (double)task.countofbytesexpectedtoreceive;     dispatch_async(dispatch_get_main_queue(), ^{         self.progressview.progress = progress;     });      self.datatask =nil; } 

everything works fine when application in foreground put application on background getting below error message.

completed error: lost connection background transfer service

you cannot use data tasks background transfers. must done using download tasks:

download tasks retrieve data in form of file, , support background downloads while app not running.

this explained in apple's documentation.

also sure check out background transfer considerations:

with background sessions, because actual transfer performed a separate process , because restarting app’s process relatively expensive, few features unavailable, resulting in following limitations...

the key here it's running in separate process cannot access data keep in memory. must routed through file.

i collected lot of information background transfer on ios in (long) blog post.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -