When an app presses the home key to exit. There is only a maximum of 5 seconds to do some work to save or clean up resources.
However, the Beginbackgroundtaskwithexpirationhandler method is called and can be executed in the background up to 10 minutes. We can use this time to clean up the local cache, send statistics, and so on.
AppDelegate.h
#import <UIKit/UIKit.h> @interface appdelegate:uiresponder <UIApplicationDelegate> @property (Strong, nonatomic) UIWindow *window;//background Long execution @property (assign, nonatomic) Uibackgroundtaskidentifier backgroundupdatetask;@ End
Appdelegate.m
When the app enters the background-(void) Applicationdidenterbackground: (uiapplication *) application { [self Beingbackgroundupdatetask]; Code that requires long execution [self endbackgroundupdatetask];} #pragma-mark-functions-(void) beingbackgroundupdatetask{ self.backgroundupdatetask = [[UIApplication Sharedapplication] beginbackgroundtaskwithexpirationhandler:^{ [self endbackgroundupdatetask; }];} -(void) endbackgroundupdatetask{ [[UIApplication sharedapplication] Endbackgroundtask: Self.backgroundupdatetask]; Self.backgroundupdatetask = Uibackgroundtaskinvalid;}
References:
http://www.devtang.com/blog/2012/02/22/use-gcd/
"Reading notes" ios-gcd-block-backstage execution