GCD-block-Background running, gcd-block-Background
When an app exits by pressing the home key, it only takes up to 5 seconds to save or clear resources. However, if you call the beginBackgroundTaskWithExpirationHandler method, you can run it in the background at most 10 minutes. We can use this time to clean up the local cache and send statistics.
AppDelegate. h
# Import <UIKit/UIKit. h> @ interface AppDelegate: UIResponder <UIApplicationDelegate> @ property (strong, nonatomic) UIWindow * window; // long-running background @ property (assign, nonatomic) implements backgroundUpdateTask; @ end
AppDelegate. m
// When the app enters the background-(void) applicationDidEnterBackground :( UIApplication *) application {[self beingBackgroundUpdateTask]; // code that requires long-term running [self endBackgroundUpdateTask];} # pragma-mark-functions-(void) beingBackgroundUpdateTask {self. backgroundUpdateTask = [[UIApplication sharedApplication] usage: ^ {[self endBackgroundUpdateTask] ;}];}-(void) usage {[[UIApplication sharedApplication] endBackgroundTask: self. backgroundUpdateTask]; self. backgroundUpdateTask = UIBackgroundTaskInvalid ;}
References:
Http://www.devtang.com/blog/2012/02/22/use-gcd/