Reprinted from: Looyao ' s blog
When the app enters the background (by pressing the home button), the app is paused, all the program logic stops, and the app resides in memory, unless forced to quit by the user or killed by the system (to ensure that the app running in the foreground has enough memory, The system will choose to kill to the other apps in the background, of course, this is not very relevant to the topic discussed in this article, this article is about how to get into the background of the app to win more running time instead of being suspended immediately. When the program enters the background, we sometimes need to do some network communication, such as sending some state data to the server, may not be too time-consuming, but will be suspended by the system, we need to buy some time to complete these operations, the following is how to win more uptime.
Suppose we need to write the logic behind the Rootcontroller in the background, then in Rootcontroller, declare an instance variable, and a method, like
@interface Rootviewcontroller:uiviewcontroller
{
Uibackgroundtaskidentifier Backgroundtask; The identifier used to hold the background run task
}
-(void) startbackgroundtask;
Realize:
-(void) Startbackgroundtask
{
UIApplication *application = [UIApplication sharedapplication];
Notification system, we need to continue to perform some logic in the background
Backgroundtask = [Application beginbackgroundtaskwithexpirationhandler:^{
The background logic is paused when the system-specified background run time is exceeded
[Application Endbackgroundtask:backgroundtask];
Backgroundtask = Uibackgroundtaskinvalid;
}];
Judging if the application fails, return
if (Backgroundtask = = Uibackgroundtaskinvalid) {
NSLog (@ "Beginground error");
Return
}
The system has been successfully won some background running time, some logic, such as network processing
Some code
}
When our tasks have been completed, such as network requests completed, it is best to inform the system that the logic of the backend has been completed
For example, the end of network processing
-(void) requestfinished
{
if (backgroundtask! = uibackgroundtaskinvalid) {
[[UIApplication sharedapplication] endbackgroundtask:backgroundtask];
Backgroundtask = Uibackgroundtaskinvalid;
}
}
The system enters the background automatically calls the -(void) Applicationdidenterbackground in appdelegate: (uiapplication *) Application This method , we're going to call Rootcontroller 's startbackgroundtask method here manually
-(void) Applicationdidenterbackground: (uiapplication *) application
{
[Rootcontroller Startbackgroundtask];
}
In this way, we can continue to run the logic we need to handle in the background, there are two points to note here:
1. The app can only run for up to 10 minutes in the background.
2, if the system is more than the time allowed to call Endbackgroundtask: This method continues to execute the logic, the app will be killed by the system.
IOS app goes backstage to get more running time