beginBackgroundTaskWithExpirationHandler: method of UIApplication object to request for additional time
How to fix some common Xcode Errors
Top 5 Simple iOS Interview Questions And Answers
iOS Interview Questions And Answers part 16
Top iOS interview Questions And Answers Collection
Top Interview Questions And Answers in Objective C
Calling either of these methods delays the suspension of your app temporarily, giving it a little extra time to finish its work.
Make sure that you are calling the endBackgroundTask: to inform the system that you have finished your task,now the app can be sent to suspended state.
How beginBackgroundTaskWithExpirationHandler: Works
beginBackgroundTaskWithName:expirationHandler: or beginBackgroundTaskWithExpirationHandler: generates a unique token that corresponds to a specific task,when called by the user.
Note:
endBackgroundTask: tells the system that the task has been finished.If the user fails to call this method,system can lead to termination of app at any time.
The request to start a background task includes an expiration handler just in case the task takes too long
You can use the below code in applicationDidEnterBackground: method.
- (void)applicationDidEnterBackground:(UIApplication *)application{
UIApplication* app = [UIApplication sharedApplication];
task = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:task];
task = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
NSLog(@"Started background task timeremaining = %f", [app backgroundTimeRemaining]);
if (connectedToNetwork) {
// do work son...
}
[app endBackgroundTask:task];
task = UIBackgroundTaskInvalid;
});
}
You can run the task in background for a time limit of 10-20 minutes. It only supports supports versions of iOS iOs 4