We can use beginBackgroundTaskWithExpirationHandler to ensure that a task doesn't get stopped abruptly This is how I used
- (void)didReceiveObjList:(CFDataRef)message
- (void)didReceiveObjList:(CFDataRef)message
{
// Received List Of Objects. Processing the list can take a few seconds.
// So doing it in separate thread with expiration handler to ensure it gets some time to do the task even if the app goes to background.
UIApplication *application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self processObjList:message];
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
NSLog(@"Main Thread proceeding...");
}
No comments:
Post a Comment