这段代码可以放在
- (void)applicationWillResignActive:(UIApplication *)application
或者 - (void)applicationDidEnterBackground:(UIApplication *)application 中
bgTask = [applicationbeginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask =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, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask =UIBackgroundTaskInvalid;
});