ios - Chaining background NSURLSession uploads -
has been successful in chaining nsurlsession background uploads?
i trying upload huge video file in 5 mb parts using background upload of nsurlsession. uploads has in order. whole thing works fine in foreground. using afnetwoking this, , multi part upload. when app in background, first item uploads fine , starts second 1 in background (in setdidfinisheventsforbackgroundurlsessionblock of afurlsessionmanager). stops abruptly (my best guess in 30 seconds, app woken in background has max lifetime of 30 sec) , nothing happens. expected second session finish in background , call third etc - chain behaviour, not seem work.
i have tried adding file parts single nsurlsession in 1 go httpmaximumconnectionsperhost = 1 - works fine , uploads full file in parts. file parts picked in random order, i.e. part 1 gets uploaded, part 5, part 3, part 10 etc …. tried adding in nsoperationqueue dependency between operations , seems mess entire thing - upload not work @ all.
i know video file can uploaded single file in background, server expects in 5 mb parts. hence guess option chain uploads, or add parts nsurlsession, make sure uploaded in order added.
any appreciated.
my code:
- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. nsurlsessionconfiguration *config = [nsurlsessionconfiguration backgroundsessionconfigurationwithidentifier:[nsstring stringwithformat:@"%d", rand()]]; afurlsessionmanager *manager = [[afurlsessionmanager alloc] initwithsessionconfiguration:config]; config.httpmaximumconnectionsperhost = 1; [manager setdidfinisheventsforbackgroundurlsessionblock:^(nsurlsession *session) { dispatch_async(dispatch_get_main_queue(), ^{ // call completion handler tell system there no other background transfers. // completionhandler(); [self upload]; }); }]; } - (ibaction)start:(id)sender { [self upload]; } -(void) upload { nsstring *filepath = [[nsbundle mainbundle] pathforresource:@"sample" oftype:@"mp4"]; afhttprequestserializer *serializer = [afhttprequestserializer serializer]; nsdictionary *parameters = [nsdictionary dictionarywithobjectsandkeys:@"234", @"u", @"sample.mp4", @"f",nil]; nsmutableurlrequest *request = [serializer multipartformrequestwithmethod:@"post" urlstring:urlstring parameters:parameters constructingbodywithblock:^(id<afmultipartformdata> formdata) { [formdata appendpartwithfileurl:[nsurl fileurlwithpath:filepath] name:@"data" filename:@"sample.mp4" mimetype:@"video/mp4" error:nil]; } error:nil]; __block nsstring *tempmultipartfile = [nstemporarydirectory() stringbyappendingpathcomponent:@"test"]; tempmultipartfile = [tempmultipartfile stringbyappendingstring:[nsstring stringwithformat:@"%d", rand()]]; nsurl *filepathtemp = [nsurl fileurlwithpath:tempmultipartfile]; __block nsprogress *progress = nil; [serializer requestwithmultipartformrequest:request writingstreamcontentstofile:filepathtemp completionhandler:^(nserror *error) { nsurlsessionuploadtask *uploadtask = [manager uploadtaskwithrequest:request fromfile:filepathtemp progress:&progress completionhandler:^(nsurlresponse *response, id responseobject, nserror *error) { nslog(@"request--> %@.\n response --> %@ \n%@", request.url.absolutestring ,responseobject, error? [nsstring stringwithformat:@" error: %@", [error localizeddescription]] : @""); //lets know result including failures [[nsfilemanager defaultmanager] removeitematpath:tempmultipartfile error:nil]; }]; [uploadtask resume]; [manager settaskdidsendbodydatablock:^(nsurlsession *session, nsurlsessiontask *task, int64_t bytessent, int64_t totalbytessent, int64_t totalbytesexpectedtosend) { nslog(@"uploading"); }]; }]; }
well, reached out apple clarifications on chaining background uploads - not possible in ios.nsurlsession has resume rate limiter prevents apps executing chained tasks in background explained in https://forums.developer.apple.com/thread/14854. instead, apple suggests batch transfers or other options https://forums.developer.apple.com/thread/14853. other thing asking order multiple tasks in upload queue - i.e, force nsurlsession upload tasks in order in added. pointed dgatwood, using nsoperationqueue not possible , apple mentioned same.as mentioned eskimo in response mail "nsurlsession not guarantee run requests in order , there’s no way enforce that." pretty left option less on original problem.
Comments
Post a Comment