Ios_ Background Run policy 1

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.