1. Release memory when entering the background
If the app does not have an arc mechanism enabled, the program needs to turn the reference count of those resources that need to be freed to 0 when the app enters the background, allowing the system to reclaim those resources.
When the app goes to the foreground, the system needs to re-restore these resources.
If the app has the ARC mechanism enabled, the program will apply nil to the variables that need to be freed when the app enters the background. When the application is transferred to the foreground,
The system needs to re-restore these resources.
2. Request more background time
When the app is in the background, do not perform more than 5 seconds in the main thread, and the app may be removed from memory if it takes too much time (i.e. Applicationdidenterbackground: the execution of the method spends too much time) in the background.
If the application is performing file downloads or file transfers, when the app is in the background, if the task has not finished executing, the task will be paused when the app is in the background. Do not force in Applicationdidenterbackground: method to complete the task directly----because this causes the app to take too much time to get into the background, the iOS system may remove the app directly from memory. The correct approach is: to Applicationdidenterbackground: method as the platform, Tell the system to enter the background or more tasks to complete, so that the system to request more background time. In this way, when our app is in the background, even if the user is using another app, our app can be saved in memory as long as the system is still memory, and the iOS system will keep the app running for a while.
Uiapplicationwillenterforegroundnotification will be sent to the notification center when the app is transferred to the foreground
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector: @selector (enterback:) Name: Uiapplicationwillenterforegroundnotification
Object:[uiapplication Sharedapplication]];
Uiapplicationdidenterbackgroundnotification will be sent to the notification center when the app is transferred to the foreground
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector: @selector (enterback:) Name: Uiapplicationdidenterbackgroundnotification
Object:[uiapplication Sharedapplication]];
-(void) Applicationdidenterbackground: (uiapplication *) application
{
[[Nsnotificationcenter defaultcenter]postnotificationname:@ "Stopcamera" object:nil];
Backgroundtask = [Application Beginbackgroundtaskwithexpirationhandler: ^{
If the timeout is exceeded, the block will be called
Ydlog (@ "Time Remaining:%f", [Application backgroundtimeremaining]);
Dispatch_async (Dispatch_get_main_queue (), ^{
if (backgroundtask! = uibackgroundtaskinvalid) {
Ydlog (@ "Time Remaining:%f", [Application backgroundtimeremaining]);
Do whatever needs to is done
[Application Endbackgroundtask:backgroundtask];
Backgroundtask = Uibackgroundtaskinvalid;
}else{
Ydlog (@ "Time Remaining:%f", [Application backgroundtimeremaining]);
}
});
}];
Ydlog (@ "%@", @ "reached the end of Applicationdidenterbackground-i ' M done!");
}
IOS--Background related