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