Another use of GCD is to allow programs to run longer in the background.
When you do not use GCD, when the app is left on the home button, the app only has a maximum of 5 seconds to do some saving or cleanup work. But after using GCD, the app has a maximum of 10 minutes to run in the background for a long time. This time can be used to clean up the local cache, send statistics and other work.
The sample code that lets the program run long in the background is as follows:
AppDelegate.h file
@property (Assign, nonatomic) Uibackgroundtaskidentifier backgroundupdatetask;
APPDELEGATE.M file
-(void) Applicationdidenterbackground: (uiapplication *) application
{
[Self beingbackgroundupdatetask];
Add the code that you need to run long
[Self endbackgroundupdatetask];
}
-(void) Beingbackgroundupdatetask
{
Self.backgroundupdatetask = [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{
[Self endbackgroundupdatetask];
}];
}
-(void) Endbackgroundupdatetask
{
[[UIApplication sharedapplication] endBackgroundTask:self.backgroundUpdateTask];
Self.backgroundupdatetask = Uibackgroundtaskinvalid;
}
Ios_ Background Run policy 1