Introduction to iOS's multitasking Mechanism

Source: Internet
Author: User

For 320ProgramPersistence is very interesting, so I first studied the IOS multitasking mechanism. for most applications, the program compiled with the latest SDK itself supports multiple tasks-Press the Home Key to run the program in the background (but note that the program will not run at this time, it is only in the background to facilitate its re-entry into the active state, which is different from the multi-task in our concept ). for some applications, you need to make full use of IOS's multitasking features, such as game applications and applications requiring network connection.

(For more information, see www.cnblogs.com/scorpiozj)

The multitasking mechanism was introduced by Apple in ios4. We first created a new project and checked the content in the appdelegate file:

APP cycle

 -  (Bool) Application :( uiapplication  *  ) Application didfinishlaunchingwitexceptions :( nsdictionary  *  ) Launchoptions {

// Override point for customization after application launch.

// Set the View Controller as the window's Root View Controller and display.

Return Yes;
}
- ( Void ) Applicationwillresignactive :( uiapplication * ) Application {
/*
Sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games shocould use this method to pause the game.
*/
}


- ( Void ) Applicationdidenterbackground :( uiapplication * ) Application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationwillterminate: when the user quits.
*/
}


- ( Void ) Applicationwillenterforeground :( uiapplication * ) Application {
/*
Called as part of transition from the background to the inactive state: here you can undo changes of the changes made on entering the background.
*/
}


- ( Void ) Applicationdidbecomeactive :( uiapplication * ) Application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previusly in the background, optionally refresh the user interface.
*/
}


- ( Void ) Applicationwillterminate :( uiapplication * ) Application {
/*
Called when the application is about to terminate.
See also applicationdidenterbackground :.
*/
}

The above functions are the functions introduced in ios4 to support multi-task running. From the function name, we can guess some possible states of the program: Background, active.
Figure 1 shows the lifecycle of a program.

Figure 1

Let's take a look: the first time the program was started, from not
When the running status enters the active stage, the program will call two functions:

    1. Didfinishlaunchingwitexceptions: and
    2. Applicationdidbecomeactive:

Click the Home key, and the program enters the background until it is suspended. The program calls the following functions in sequence:

    1. Applicationwillresignactive:
    2. Applicationdidenterbackground:

When you double-click the Home Key to enter the program, the program will call the following one in sequence:

    1. Applicationwillenterforeground:
    2. Applicationdidbecomeactive:

Note that the background contains two statuses: Running and suspend. here, running is not a program running in the true sense, but an applicationdidenterbackground: The running part. Generally, the system will not execute this function too much time, after execution, the program will be suspended in the background. this is the vast majority of IOS multi-task scenarios. However, in three cases, Apple allows you to run in the background.

    • Music
    • Location
    • VoIP

You can set in the plist file in the program, 2:

Figure 2

In this case, apart from the three services that we usually think of as a multi-task mechanism, the multi-tasks of other programs are the state of the programs mentioned above. I will not discuss why Apple wants to do this here. I just want to explain a little. In this case, at least many tasks are implemented on the surface: for example, I want to see the weather again when I play Angry Birds, then I want to play the birds again, so it is easier to switch, because the program is suspended in the background, or is running in the memory, so that the startup will be faster.

When the program calls the corresponding function, the system will send the corresponding notification. At this time, the app should save the app status or read the Historical Status of the app in a timely manner, so as to better present the user experience.

As mentioned before, it takes a short time for the program to enter the background, and sometimes the operations to be performed by the app are not ready yet. In this case, we can use beginbackgroundtaskwithexpirationhandler: to process operations that may take a long time.

Summary:

The so-called multi-task in IOS is not what we usually think of as a multi-task. iOS only allows three services to run in the background. The other is to facilitate switching between multiple apps.

(Details on how to implement multiple tasks, such as what operations should be prioritized when entering the background program will be explained in the future .)

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.