ios - Crash with NSURLSessionUploadTask when trying to upload in background -
in app try upload data server when app enters background. code using:
session
self.session = [self backgroundsession];
this how session set up
- (nsurlsession *)backgroundsession { static nsurlsession *session = nil; static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration backgroundsessionconfiguration:@"com.uploadsession"]; configuration.httpmaximumconnectionsperhost = 1; session = [nsurlsession sessionwithconfiguration:configuration delegate:self delegatequeue:nil]; }); return session; }
initiate upload
- (void)applicationdidenterbackground:(uiapplication *)application { [self uploadpossibledrives]; }
upload
// start uploading remaing gps log in th right order: nsoperationqueue *queue = [[nsoperationqueue alloc] init]; [queue setmaxconcurrentoperationcount:1]; (int = 0; < arrayofgpslogchunks.count; i++) { // ensure chunks sent in right order // add operation block queue: [queue addoperationwithblock: ^ { nsdata *requestdata; nsmutableurlrequest *request; if (i == 0) { nslog(@"initial drive upload %i",id.integervalue); requestdata = [nsjsonserialization datawithjsonobject:historytoupload options:nsjsonwritingprettyprinted error:nil]; request = [[nsmutableurlrequest alloc] initwithurl:[nsurl urlwithstring:kinitialuploaddriveurl]; [request sethttpmethod:@"post"]; [request setvalue:@"application/json" forhttpheaderfield:@"content-type"]; [request setvalue:[nsstring stringwithformat:@"%d", [requestdata length]] forhttpheaderfield:@"content-length"]; } else { nslog(@"chunk %i",i); nsmutabledictionary *chunk = [[nsmutabledictionary alloc] init]; [chunk setobject:driveid forkey:@"driveid"]; [chunk setobject:[arrayofgpslogchunks objectatindex:i] forkey:@"gpslog"]; requestdata = [nsjsonserialization datawithjsonobject:chunk options:nsjsonwritingprettyprinted error:nil]; request = [[nsmutableurlrequest alloc] initwithurl:[nsurl urlwithstring:kuploaddriveschunkurl]]; [request sethttpmethod:@"post"]; [request setvalue:@"application/json" forhttpheaderfield:@"content-type"]; [request setvalue:[nsstring stringwithformat:@"%d", [requestdata length]] forhttpheaderfield:@"content-length"]; } nslog(@"_session: %@",self.session); nslog(@"request: %@",request); nslog(@"requestdata: %lu",(unsigned long)requestdata.length); nslog(@"uploadtask: %@",self.uploadtask); self.uploadtask = [self.session uploadtaskwithrequest:request fromdata:requestdata]; [self.uploadtask resume]; if (i == arrayofgpslogchunks.count-1) { // gps logs uploaded save state 'uploaded server': nslog(@"finished sending server!"); [self setsenttoserverfordriveid:id]; } }]; }
error
now here error get:
i hope can me out. have looked know cant seem figure out ist going wrong. tried uploading without using blocks result same.
any appreciated!
for nsurlsession created backgroundsessionconfiguration, have use uploadtaskwithrequest:fromfile:
.
see question: nsurlsession , stream upload in background
Comments
Post a Comment