When we use APP, most people will know that there are: simple state in use; Leave the application through the home button, but do not close the application, that is, background state; As well as calling up the daemon management after swiping up to “kill” the application, three states. We developers will have a better understanding of what we’re doing. More and deeper knowledge still needs to be summarized after future exploration. Next, I will summarize what I have learned about the life cycle of APP in the initial stage.

The five states of an application

1. Not running:

The application is not started, or the application is running but stopped by the system.

2. Inactive:

The application is currently running in the foreground, but does not receive events (other code may be currently executing). Typically, whenever an application switches from one state to a different state, the transition is short-lived. Such as when the user locks the screen or is notified to respond to another event.

3. Active:

The current application is running in the foreground and receiving events. This is the normal state in which the application is running in the foreground.

4. Background:

The application is in the background and executing code. Most apps that are about to enter a Suspended state will enter it briefly first. Except for some applications that can stay in this state for a long time after special request. There are some applications that go straight to the background state when they start.

5. Suspended:

The application is in the background and has stopped executing code. The system automatically changes the program to this state without notification. When suspended, programs remain in memory, and when system memory is low, the system removes pending programs to provide more memory for foreground programs.

IOS program execution process

An overview can be made by the following figure:

The methods that listen for system events are in the appdelegate. m file, and the callbacks of the method proxies are interpreted as:

/ / tell the agent to start preparing start running - basically completed the program (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *)launchOptionsCopy the code
/ / when the application is going into the inactive state, in the meantime, the application does not receive messages or events, such as calling - (void) applicationWillResignActive (UIApplication *) applicationCopy the code
/ / when the application in the active state, this method is the method with the above instead - (void) applicationDidBecomeActive: (UIApplication *) applicationCopy the code
// called when the application is pushed to the background. So continue to set the background, in this function can be set - (void) applicationDidEnterBackground: (UIApplication *) applicationCopy the code
/ / when the program is called from the background will be returned to the front desk, the methods of this approach to keep up with the opposite - (void) applicationWillEnterForeground (UIApplication *) applicationCopy the code
/ / when the program will exit is invoked, it is usually used to store data and some cleanup work before - (void) applicationWillTerminate: (UIApplication *) applicationCopy the code

Add here the NSLog print method that is executed when the related operation is performed

Methods are executed from top to bottom in each operation.

When the program is started:

function:-[AppDelegate application:didFinishLaunchingWithOptions:] line:25 content:-[AppDelegate application:didFinishLaunchingWithOptions:]
Copy the code
function:-[AppDelegate applicationDidBecomeActive:] line:52 content:-[AppDelegate applicationDidBecomeActive:]
Copy the code

When you press the home button:

function:-[AppDelegate applicationWillResignActive:] line:33 content:-[AppDelegate applicationWillResignActive:]
Copy the code
function:-[AppDelegate applicationDidEnterBackground:] line:40 content:-[AppDelegate applicationDidEnterBackground:]
Copy the code

Double-click the home button or click the icon to open the program again:

function:-[AppDelegate applicationWillEnterForeground:] line:46 content:-[AppDelegate applicationWillEnterForeground:]
Copy the code
function:-[AppDelegate applicationDidBecomeActive:] line:52 content:-[AppDelegate applicationDidBecomeActive:]
Copy the code

Knowing several states and cycles in APP operation, we can optimize and deal with some of the situations in response.

For example, when an unexpected phone call or other event causes the program to temporarily enter Inactive state, we can use theapplicationWillResignActive:Method:

  • Stop timers and other periodic tasks
  • Stop any running requests
  • Pause the video
  • If it’s a game, pause it
  • Reduce the frame rate of OpenGL ES
  • Suspend any distribution queues and unimportant operation queues (you can continue processing network requests or other time-sensitive background tasks).

When the program enters the background:

Should achieve:

  • Save user data or status information, all files or information not written to disk, when entering the background, is finally written to disk, because the program can be killed in the background
  • Free as much memory as possible

In the background, each application should free as much memory as possible. The system keeps as many applications running in the background as possible. So the background will automatically reclaim some of the memory that the system helps you to open up in order to reduce the memory occupied by the program, such as:

  • The system reclaims the backup storage of Core Animation
  • Remove any cached images referenced by the system
  • Remove the system management data cache strong reference

We can also make some efforts:

Remove strong references to some objects so that the compiler can reclaim the memory:

  • Image objects
  • You can reload large video or data files
  • Any useless object that can be easily created

When the program terminates:

At the termination of the APP, the system will call applicationWillTerminate: method, in which we can make some saving data or status, or some cleaning work. Methods have a maximum time limit of 5 seconds for these tasks, and if there are any unfinished tasks beyond that time, your program will be terminated and wiped from memory.

And into a state like the background and the program terminates, if need long time running tasks, can call beginBackgroundTaskWithExpirationHandler method to request the background of time and start a thread to run a long running task.