iOS implementation in the background to achieve background tasks

Source: Internet
Author: User

-(void) Applicationdidenterbackground: (uiapplication *) application

{

//Use this method to release shared resources, save user data, invalidate timers, and store enough Applicati On the state information to the restore your application to the it is terminated later.

//If Your application supports background execution, this method is called instead of Applicationwilltermina Te:when the user quits.

nsassert(self-bgtask = = Uibackgroundtaskinvalid, nil);

Self - Bgtask = [Applicationbeginbackgroundtaskwithexpirationhandler: ^{

Synchronize the cleanup call on the main thread in case

The task catully finished at around the same time.

Dispatch_async (dispatch_get_main_queue(), ^{

if (uibackgroundtaskinvalid! =self --bgtask) {

[Application endbackgroundtask:Self--bgtask];

Self - Bgtask = Uibackgroundtaskinvalid;

}

});

}];

Start the long-running task and return immediately.

Dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default ,0),

^{

// do the work assoicated with the task.

// Synchronize The cleanup all on the main thread in C Ase

// the task catully finished at around the same time.

dispatch_async ( Dispatch_get_main_queue(), ^{

NSLog(@ "Background time:%f", [[uiapplication sharedapplication] backgroundtimeremaining]);

if ( Uibackgroundtaskinvalid! =self--bgtask) {

[Applicationendbackgroundtask:Self--bgtask];

self- >bgtask =uibackgroundtaskinvalid;

}

});

});

}

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.