iOS deeply understands the +load method from 4 aspects

Estimated read time 6 min read

Ask questions from the following aspects to understand +loadthe understanding and experience of using the method:

  1. basic concept :
    • Please explain what is a method in Objective-C +loadand when is it called?
    • +loadWhat is the difference between method and +initializemethod?
  2. Performance considerations :
    • How might usage +loadimpact app startup time? How to optimize?
  3. Deep understanding :
    • +loadHow are methods implemented in the Objective-C runtime mechanism ?
    • +loadWhat special role does the method have when loading a dynamic library?
  4. Why should we design the +load method separately?

1. Basic concepts :

+loadmethod

In Objective-C, +loada special class method that is automatically called when a class or category is loaded into the runtime. This process occurs early in the application startup, even mainbefore the function is executed.

+loadFeatures of the method include:

  • Automatic call: No need to trigger manually.
  • Early execution: Execute before all class methods, even before mainfunctions.
  • Uniqueness: +loadThe method will only be called once for each class and category.

+initializemethod

+initializeAlso a special class method that is automatically executed before the first method of the class is called. Unlike +load+initializeit is called when the class is used for the first time, not when it is loaded.

+initializeFeatures of the method include:

  • Delayed call: Not executed until the first method of the class (such as an instance method or class method) is called.
  • Called along the inheritance chain: If a class does not implement +initializea method, but its parent class does, then the parent class’s +initializemethod will be called. If the subclass implements its own method, the parent class method will not be automatically called. You need to manually call the parent class method in the +initializesubclass method.+initialize

the difference

  • Calling timing : +loadCalled immediately when the class is loaded, but +initializebefore the first method of the class is called.
  • Number of executions : +loadCalled only once for each class and classification, +initializecalled on the first use of each class, and called only once.
  • Inherited behavior : In +initialize, if the subclass does not implement this method, the parent class’s +initializewill be called. +loadInstead, calls are made to classes and categories respectively, regardless of inheritance relationships.

Since +loadand +initializehave different calling timings and behaviors, they are suitable for different scenarios. +loadTypically used to perform some early setup or initialization tasks, but +initializemore suitable for lazy loading or initializing class-level data.

2.Performance considerations :

+loadThe impact of methods on application startup time

+loadMethods are called during the early stages of app startup, so if +loadtime-consuming operations are performed within a method, it may significantly increase your app’s startup time. Since methods are automatically called when a class or class is loaded into the runtime, this can also have a negative impact on startup time +loadif there are a large number of classes or classes that implement the method.+load

How to optimize

  1. Reduce +loadmethod usage+load : Execute code in methods only when absolutely necessary . Consider whether the initialization code can be moved elsewhere, such as +initializewithin a method or the application’s startup flow.
  2. Avoid time-consuming operations : +loadAvoid performing time-consuming operations in methods, such as network requests, disk I/O operations, or large amounts of calculations.
  3. Asynchronous execution : If you need to perform some initialization operations, you can consider executing these operations asynchronously to reduce the impact on startup time.
  4. Performance analysis : Use performance analysis tools (such as Xcode’s Instruments) to detect performance bottlenecks when the application starts and determine whether there is +loada method that causes the problem.

3. Deep understanding :

+loadHow are methods implemented in the Objective-C runtime mechanism ?

In Objective-C, +loada method is a special class method that is automatically called when a class or category is loaded into the runtime. This process is managed by Objective-C’s runtime system. Specifically, when a class or classification is added to the runtime, the runtime system checks whether the class or classification implements +loadthe method. If implemented, the runtime system will mainautomatically call this method during the loading phase (before the function is executed).

+loadThe order in which methods are called follows the following rules:

  1. The methods of the parent class +loadare executed before the child class.
  2. Class +loadmethods are executed before categories (Category).
  3. The order in which methods of different classes and categories +loadare executed is related to the order in which they are compiled and linked into the application.

+loadMethod implementations are special code segments generated by the compiler at compile time that are automatically executed at runtime.

+loadWhat special role does the method have when loading a dynamic library?

When a dynamic library (such as a dynamically linked Framework or Bundle) is loaded into an application, +loadthe methods of all classes and categories in the library will be automatically called. This provides an opportunity for the dynamic library to perform some initialization operations, such as registering classes, initializing global variables, etc., without requiring explicit calls from the application.

This feature is very useful in some scenarios, such as:

  • Plug-in system : Dynamic libraries can be loaded into applications as plug-ins, and +loadplug-in classes are automatically registered through methods without additional initialization by the application.
  • Framework initialization : The framework can +loadperform some necessary initialization operations in methods to ensure that the framework is ready before being used in other parts of the application.

In general, +loadthe method provides an opportunity to automatically execute initialization code when the dynamic library is loaded, which allows the dynamic library to configure its own environment immediately when loading.

Why should we design the +load method separately?

+loadMethods are designed separately in Objective-C, mainly to provide a mechanism so that classes and categories (Category) can execute some specific initialization code during the runtime loading phase. This design has several important purposes and advantages:

  1. Automatic execution : +loadThe method will be automatically called when the class or category is loaded into the runtime, without manual triggering. This ensures that no matter when a class or class is introduced, their initialization code will be executed.
  2. Early Execution : +loadMethods are called very early in the application’s startup process, even before mainfunctions. This makes it an ideal place to perform some pre-configuration or environment setup.
  3. Applies to classes and categories : +loadmethods can be implemented in either classes or categories. This provides a flexible way to add initialization logic through classes without modifying the original class code.
  4. No need to explicitly call : Since +loadthe method is automatically called, developers do not need to explicitly call this method in the code, reducing the possibility of errors.
  5. Initialization of dynamic libraries : +loadThe method is especially important for dynamic libraries because it allows the dynamic library to perform some necessary initialization operations when loading without requiring the application to make additional calls.

However, it is important to note that while +loadthe method provides convenience, it also has some disadvantages, such as potentially increasing application startup time and making code difficult to understand and maintain when used incorrectly. Therefore, +loadyou need to weigh the pros and cons when using a method and ensure that its usage scenario is reasonable.

You May Also Like

More From Author

+ There are no comments

Add yours