ipad - perform cleanup task while going to background in iOS -
i have perform long running clean operation while application going background. clean operation network transaction , take more 5 seconds using beginbackgroundtaskwithexpirationhandler: api , working fine.
below adding code better clarity..
- (void)applicationdidenterbackground:(uiapplication *)application { @synchronized(self) { bgtask = [application beginbackgroundtaskwithexpirationhandler:^{ [application endbackgroundtask:bgtask]; bgtask = uibackgroundtaskinvalid; }]; dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_high, 0), ^{ [self performcleanupoperation]; [application endbackgroundtask:bgtask]; bgtask = uibackgroundtaskinvalid; }); } } - (void) performcleanupoperation { // cleanup network operation [(nsobject *)self performselectoronmainthread:(@selector(cleanupdidfinish)) withobject:nil waituntildone:no]; } - (void) cleanupdidfinish { dispatch_async(dispatch_get_main_queue(), ^(void){ [self savecontext]; [(customuiapplication *)[uiapplication sharedapplication] logoutwithalert:no]; [[uiapplication sharedapplication] endbackgroundtask:bgtask]; bgtask = uibackgroundtaskinvalid; }); }
now problem facing is, when bring application foreground seeing old screen app before going background. , navigating login screen old screen.
any idea why not showing login screen when application relaunches, have loaded login viewcontroller inside cleanupdidfinish.
the code you've written after beginbackgroundtaskwithexpirationhandler
not chance execute because performed asynchronously. tasks declared background tasks need synchronous in order executed way expect them here.
Comments
Post a Comment