Two background-related callback Processes

Source: Internet
Author: User

1. AboutProgramBackground Data Processing

The time for processing data in the IOS system after the program enters the background state is 5 s, but this time is very short. If you need to process it in the background for a long time, you can apply to the system for an extension of this time, which can be up to 10 minutes. During this period, the program can perform related data operations in the background. After testing, you can perform background download tasks.

CodeAdd the following to the program delegate:

# Pragma mark-

# Pragma mark background task handle

-(Void) applicationdidenterbackground :( uiapplication *) Application

{

Nslog (@"!!! % @ ", Nsstringfromselector (_ cmd ));

Uiapplication * APP = [uiapplication sharedapplication];

_ Block uibackgroundtaskidentifier taskid;

Taskid = [App beginbackgroundtaskwithexpirationhandler: ^ {

Nslog (@ "background task timed out and exited ");

[App endbackgroundtask: taskid];

}];

If (taskid = uibackgroundtaskinvalid)

{

Nslog (@ "failed to enable background tasks ");

Return;

}

Dispatch_async (dispatch_get_global_queue (0, 0), ^ {

Nslog (@ "Maximum backend task data time limit: % F seconds", app. backgroundtimeremaining );

[Nsthread sleepfortimeinterval: 10];

Nslog (@ "Maximum backend task data time limit: % F seconds", app. backgroundtimeremaining );

[App endbackgroundtask: taskid]; // notifies the system that the background task has been processed.

});

}

Program output:

11:28:06. 895 backgrounddemo [46774: 707]! Applicationdidenterbackground:

11:28:06. 904 backgrounddemo [46774: 1a03] The maximum backend task time is 599.879219 seconds.

11:28:16. 924 backgrounddemo [46774: 1a03] The maximum backend task time is 589.859110 seconds.

2. Handling of insufficient memory warnings received in the background

When the program receives a memory warning, it will implicitly call didreceivememorywarning for processing, and may release some resources, such as background images and controls, after the program is transferred from the background to the execution status, it will be displayed. For example, it is determined that the program is not due to memory leakage but is indeed due to the excessive number of programs running in the system (the above function, the program runs in the background for a maximum of 10 minutes without releasing the memory). You can reload the didreceivememorywarning function to disable processing or manually release unused resources.

The Code is as follows:

@ Implementation uiviewcontroller (memorywarning)

-(Void) didreceivememorywarning

{

// Executed when receiving the memory warning

// Do nothing

}

@ End

--- By yuzhang2

Related Article

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.