【iOS】—— APP startup process

APP startup process

cold start and warm start

Cold start: At startup, the App process is not in the system, and a new process needs to be started.
Hot start: At startup, the process of the App is still in the system, and there is no need to start a new process.

APP complete startup process

It is mainly divided into three stages:

  • main()Before function execution ( pre-mainphase)
  • main()After function execution (from mainfunction execution to setting self.window.rootViewControllerexecution completion)
  • After the first screen rendering is completed (from self.window.rootViewControllerthe completion of execution to didFinishLaunchWithOptionsthe end of the method scope)

1. What the system will do before the main function is executed:

  • Load the executable. (All .o files in App)
  • Load the dynamic link library, perform rebase pointer adjustment and bind symbol binding.
  • ObjC runtime initialization. Including: ObjC-related Class registration, category registration, selector uniqueness check, etc.
  • initialization. Including: executing the +load() method, calling a function decorated with attribute((constructor)), creating C++ static global variables, etc.
    insert image description here
    After the App starts, first, the system kernel (Kernel) creates a process.
    Second, load the executable. (Executable files refer to files in Mach-O format, that is, the collection of all .o files in the App.) At this time, you can get the path of dyld (dyld is Apple's dynamic linker).
    Then, load dyld, which is mainly divided into 4 steps:
    1. load dylibs: At this stage dyld will analyze the dylibs that the application depends on, find its mach-o files, open and read these files and verify their validity, and then find the code The signature is registered to the kernel, and finally mmap() is called for each segment of the dylib.
    2. Rebase/bind: Perform rebase pointer adjustment and bind symbol binding.
    3. ObjC setup: runtime initialization. Including ObjC-related Class registration, category registration, selector uniqueness check, etc.
    4. Initializers: Call the +load method of each ObjC class and classification, call the function modified by attribute ((constructor)), and create C++ static global variables.

2. After the main function is executed:

The stage after the execution of the main function refers to: from the execution of the main function to the completion of the first screen rendering related methods in the didFinishLaunchingWithOptions method of appDelegate.
That is, from the execution of the main function to the completion of setting self.window.rootViewController.
insert image description here

3. After the first screen rendering is completed:

The stage after the first screen rendering is completed refers to: all method executions after the first screen rendering is executed within didFinishLaunchingWithOptionsthe scope of the method . i.e. from setup to end of method scope.

self.window.rootViewControllerdidFinishLaunchWithOptions

At this stage, the first screen has been rendered.

Things to do:

  • Initialize some functions that are not needed for the first screen display.
  • Optimize the main thread, and deal with the method that will block the main thread first, so as not to affect the user's subsequent operations.

Mach-O

mach-oIs iOS/macOSthe format of binary files, mach-oand is divided into several different types. This article describes common mach-ofile types and how they differ.
In Xcode->Build Setting ->Mach-O Type, we can choose from the following types:

  • Executable(The product is an ipa package)
  • Dynamic Library(The product is a dynamic library)
  • Bundle(The product is a bundle file)
  • Static Library(The product is a static library)
  • Relocatable Object File(redirect file)

Compile and link process: 【iOS】——compile and link

Guess you like

Origin blog.csdn.net/m0_62386635/article/details/132018355