Talking about the iOS life cycle from practice

Source: Internet
Author: User

Talking about the iOS life cycle from practice

The personal feeling lifecycle is an important concept both on Android and iOS because we can do a lot of pre-loading or processing in the state of each claim cycle. So here's a summary of some of the states in Viewcontroller and Appdelegate.

Status of the application

Look at this picture:

    • Foreground: Front Desk

    • Background: Backstage

    • Not running: application does not start

    • Inactive: The application runs in the foreground but does not receive an incident response

    • Active: The application runs in the foreground to accept the event response

    • Background: Running in the background and executing code

    • Suspended: Running in the background but not running code

App Launch

Basically, each state change invokes some delegate objects to respond to the current state. Let's introduce these delegate objects and illustrate some of the practical uses

-(BOOL) Application: (UIApplication *) application willfinishlaunchingwithoptions: (nsdictionary *) launchOptions{

NSLog (@ "willfinishlaunchingwithoptions");

return YES;

}

Willfinishlaunchingwithoptions is the state of the application when it is started, which is a state when the page is going to be displayed.

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {

Override point for customization after application launch.

NSLog (@ "didfinishlaunchingwithoptions");

return YES;

}

Didfinishlaunchingwithoptions is the last operational opportunity before the application is displayed

You can look at the verification:

Since the app does what it does before it shows the app to the user, it's a matter of course we can do some basic initialization here, but it should be a lightweight initial session, otherwise it will affect the user experience. Give some examples:

    • I use the code to write the UI, without using storyboard, will be here to configure the next window

    • There are, for example, you have some kind of fabric such as plug-in for data analysis, AdMob ads and other initialization can actually be put here.

    • Even the afnetworking StartMonitor, these initialization operations can be put here.

But the analysis, willfinishlaunchingwithoptions and didfinishlaunchingwithoptions description of the same, but what is the essential difference?

Personal feeling in the use of the fact there is not much difference, but the essence of the difference I think with Android OnStart and Onresume have some similar, in Android, their differences are based on whether activity is visible this angle, is actually a preparation before and prepare to complete the difference, willfinishlaunchingwithoptions is to declare that the current process has been started but has not entered the state to save, So didfinishlaunchingwithoptions is declaring that all the basic preparations have been completed and can start, so some of the actual scenarios that we just talked about are mostly done in didfinishlaunchingwithoptions.

Restart here there is another state, look at the code:

-(void) Applicationdidbecomeactive: (uiapplication *) Application {

NSLog (@ "applicationdidbecomeactive");

}

The whole boot process is actually like this, after the app starts, the state is changed from not running to inactive, then the two methods mentioned above are called, then the inactive switch to the active state, This time will call Applicationdidbecomeactive, here will not demonstrate, interested can see for themselves, so, in the lock screen, switching back and forth the app will call the Applicationdidbecomeactive method.

The app has been interrupted

The typical possibility of interruption is that someone calls in, this time will enter the inactive state, ready to enter the background, here will call

-(void) Applicationwillresignactive: (uiapplication *) Application {

NSLog (@ "applicationwillresignactive");

}

The actual application is some pause operation:

    • For example, pause operation on video

    • Pause operation on Music

    • There may be a pause in the game.

    • Suspend some unimportant tasks, if you have a download task, you can consider ending it or pausing it, of course if nsurlsession, if we download something small, we can consider whether it

App cut back from the background

-(void) Applicationwillenterforeground: (uiapplication *) Application {

NSLog (@ "Applicationwillenterforeground");

}

The app will be cut back from the background, this time call this method, here we need to do is to consider your application need not update Ah, whether need to pull data from the server AH.

Basically you can consider doing some of the frequently needed updates here, many applications I think it is also here upload timestamp file compare with remote server, if timestamp is different, pull data.

Terminating the application

The iOS system itself has a mechanism for system memory recycling, which is when other apps start running out of memory to terminate some applications, or it may be that the time in the background does not respond to being terminated. Therefore, the system will call before terminating

-(void) Applicationwillterminate: (uiapplication *) Application {

[Self savecontext];

}

The goal is to retain some important data to facilitate the next startup recovery, to achieve a feeling that my app will always run in the background never been terminated.

The life cycle of Viewcontroller

Here is a brief overview.

-(void) Viewdidappear: (BOOL) animated{

}

-(void) Viewwillappear: (BOOL) animated{

}

The above two methods are used to register and remove notifications, such as observer, because each time this view appears, the message will call these two methods, to prevent memory leaks.

-(void) Viewdidload {

[Super Viewdidload];

Additional setup after loading the view, typically from a nib.

}

is the current VC initialization, where you can configure their own Navi bar and the like.

End

Basically that's it, in a simple summary here, the system uses main run loop to manage the event loop, deciding which objects the event will be given to and how to handle it. If you want to know more about this piece, you can look at this in-depth understanding Runloop

http://blog.ibireme.com/2015/05/18/runloop/

Life cycle in our practice of the use of the place will be a lot, there is no deep summary, because of their own limited capacity, if you have any better, or feel that this article has to change or add, give me a message, welcome to point out shortcomings and errors.

Talking about the iOS life cycle from practice

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.