[UIApplication beginBackgroundTaskWithExpirationHandler]
Marks the beginning of a new long-running background task.
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler
Parameters
- handler
- A handler to be called shortly before the application’s remaining background time reaches 0. You should use this handler to clean up and mark the end of the background task. Failure to end the task explicitly will result in the termination of the application. The handler is called synchronously on the main thread, thus blocking the application’s suspension momentarily while the application is notified.
Return Value of [UIApplication beginBackgroundTaskWithExpirationHandler]
A unique identifier for the new background task. You must pass this value to the
endBackgroundTask:
method to mark the end of this task. This method returnsUIBackgroundTaskInvalid
if running in the background is not possible.Discussion of [UIApplication beginBackgroundTaskWithExpirationHandler]
This method lets your application continue to run for a period of time after it transitions to the background. You should call this method at times where leaving a task unfinished might be detrimental to your application’s user experience. For example, your application could call this method to ensure that had enough time to transfer an important file to a remote server or at least attempt to make the transfer and note any errors. You should not use this method simply to keep your application running after it moves to the background.[UIApplication beginBackgroundTaskWithExpirationHandler]
Each call to this method must be balanced by a matching call to the
endBackgroundTask:
method. Applications running background tasks have a finite amount of time in which to run them. (You can find out how much time is available using the backgroundTimeRemaining
property.) If you do not callendBackgroundTask:
for each task before time expires, the system kills the application. If you provide a block object in the handler parameter, the system calls your handler before time expires to give you a chance to end the task.[UIApplication beginBackgroundTaskWithExpirationHandler]
You can call this method at any point in your application’s execution. You may also call this method multiple times to mark the beginning of several background tasks that run in parallel. However, each task must be ended separately. You identify a given task using the value returned by this method.
This method can be safely called on a non-main thread.
Example of [UIApplication beginBackgroundTaskWithExpirationHandler]
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
};
[self ffwButtonPressed:ffwButton];
NSLog(@"beginBG called");
[app endBackgroundTask:bgTask];
Example of [UIApplication beginBackgroundTaskWithExpirationHandler]
- (void) doUpdate
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self beginBackgroundUpdateTask];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
// Do something with the result
[self endBackgroundUpdateTask];
});
}
- (void) beginBackgroundUpdateTask
{
self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}
- (void) endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}
Example of [UIApplication beginBackgroundTaskWithExpirationHandler]
- (void)applicationDidEnterBackground:(UIApplication *)application { inBackground=YES; UIApplication* app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ while (inBackground == YES) { // check lat and lon // if its what you want call the local Notification [NSThread sleepForTimeInterval:(10*60)]; } [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); }