IOS--Background related

Source: Internet
Author: User

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

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.