IOS app enters the background to get more running time

Source: Internet
Author: User

When the app enters the background (press the Home Key), the app will be paused by the system, all program logic will be stopped, and the app will still reside in the memory, unless forced to exit by the user, or the system will kill the app (to ensure that the app running on the foreground has enough memory, the system will choose to kill other apps in the background). Of course, this is not relevant to the topic discussed in this article, this article describes how to get the app that enters the background to get more running time instead of being immediately suspended. when the program enters the background, we sometimes need to perform some network communication, such as sending some status data to the server. These operations may not be too time-consuming, but will be paused by the system, we need some time to complete these operations,
The following describes how to get more running time.
Suppose we need to write the logic in the backgroundRootcontrollerIn the rootcontroller, declare an instance variable first, and a method, similar

@ Interface rootviewcontroller: uiviewcontroller
{

Uibackgroundtaskidentifier backgroundtask; // used to save the identifier of the background running task
}

-(Void) startbackgroundtask;

Implementation:

-(Void) startbackgroundtask
{

Uiapplication * Application = [uiapplication sharedapplication];

// Notification system. We need to execute some logic in the background.

Backgroundtask = [Application beginbackgroundtaskwithexpirationhandler: ^ {

// If the specified background running time is exceeded, the background logic is paused.

[Application endbackgroundtask: backgroundtask];

Backgroundtask = uibackgroundtaskinvalid;

}];



// If the application fails, return

If (backgroundtask = uibackgroundtaskinvalid ){

Nslog (@ "beginground error ");

Return;

}



// You have successfully obtained some background running time from the system to implement some logic, such as network processing.

// Some code
}

When our task has been completed, such as the completion of network requests, it is better to notify the system that the background logic has been completed.

// For example, network processing ends
-(Void) requestfinished
{

If (backgroundtask! = Uibackgroundtaskinvalid ){

[[Uiapplication sharedapplication] endbackgroundtask: backgroundtask];

Backgroundtask = uibackgroundtaskinvalid;

}
}

The system is automatically called when it enters the background.AppdelegateIn-(Void) applicationdidenterbackground :( uiapplication *) ApplicationWe need to manually call this method here.RootcontrollerOfStartbackgroundtaskMethod

-(Void) applicationdidenterbackground :( uiapplication *) Application
{

[Rootcontroller startbackgroundtask];
}

In this way, we can continue to run the logic we need to process in the background. here we need to pay attention to two points:
1. The app can only run in the background for up to 10 minutes,
2. If it exceeds the time allowed by the system, it is not called.Endbackgroundtask:This method continues to execute the logic, and the app will be killed by the system.

References:
Uiapplication
Class reference

  • Reprinted please indicate the source: looyao's blog
  • Link: IOS
    The app enters the background to get more running time
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.