Load 和 initialize 的研究

作者: 学习路上一个远行者 | 来源:发表于2016-08-30 22:55 被阅读114次

    记录一下load方法和initialize方法的区别和用法。参考的文献分别是:

    Stack Over:http://stackoverflow.com/questions/13326435/nsobject-load-and-initialize-what-do-they-do

    简书:http://www.jianshu.com/p/9368ce9bb8f9;

    http://www.jianshu.com/p/d25f691f0b07


    1.load 方法的讲解

    (1)首先我们先看看Apple官方文档的对load方法的描述

    Invoked whenever a class or category is added to the Objective-C runtime; implement this method to perform class-specific behavior upon loading.
    The load message is sent to classes and categories that are both dynamically loaded and statically linked, but only if the newly loaded class or category implements a method that can respond.
    The order of initialization is as follows:
    All initializers in any framework you link to.
    All+loadmethods in your image.
    All C++ static initializers and C/C++__attribute__(constructor)functions in your image.
    All initializers in frameworks that link to you.

    In addition:
    A class’s+loadmethod is called after all of its superclasses’+loadmethods.
    A category+loadmethod is called after the class’s own+loadmethod.

    In a custom implementation of load you can therefore safely message other unrelated classes from the same image, but any load methods implemented by those classes may not have run yet.

    其实我对这篇中用横线消除的部分也不是很了解。但是我们可以看看加粗的句子。第一句话说明了load被调用的时机。也就是一个类或者分类被加入到runtime中的时候load方法就会被系统自动调用。可是有一个问题,项目中含有很多类或者分类,那么A类的load方法和B类的load方法那个应该被调用呢?在Xcode中有一个地方标志了那个文件应该先被调用load方法:

    图片-1

    在compile sources中文件从上倒下的顺序制定了调用load方法,当然你可以手动的更改文件load的顺序通过调换compile sources中文件的顺序。但是这个不是唯一的顺序。请不要忘记了Apple文档中说的那两句话(我加粗的下面那两句),在此基础上应该先调用父类,在调用子类。最后调用分类。

    注意事项:
    1.load方法中可以不写 [super load]; 系统会自动帮你调用父类的方法。如果你写了这句话那么系统会调用两次哦。
    2.load方法只能被调用一次。

    (2)用途
    2.1首先load方法是线程安全的。在load方法中不要使用锁机制。如果在load方法中发生现成阻塞那么App加载开启会非常慢,你可以在一个类的load方法中加入这句话sleep(10);线程睡10秒,那么你的启动页面会停留10左右。
    2.2可以在方法中对class文件进行修改,例如调换方法实现。


    2.Initialize方法讲解

    (1)文档描述

    Initializes the class before it receives its first message.
    The runtime sends initialize to each class in a program just before the class, or any class that inherits from it, is sent its first message from within the program. The runtime sends the initialize message to classes in a thread-safe manner. Superclasses receive this message before their subclasses. The superclass implementation may be called multiple times if subclasses do not implement initialize—the runtime will call the inherited implementation—or if subclasses explicitly call[super initialize]. If you want to protect yourself from being run multiple times, you can structure your implementation along these lines:
    + (void)initialize {
    if (self == [ClassName self]) {
    // ... do the initialization ...
    }
    }

    Because initialize is called in a thread-safe manner and the order of initialize being called on different classes is not guaranteed, it’s important to do the minimum amount of work necessary in initialize methods. Specifically, any code that takes locks that might be required by other classes in their initialize methods is liable to lead to deadlocks. Therefore you should not rely on initialize for complex initialization, and should instead limit it to straightforward, class local initialization.
    initialize is invoked only once per class. If you want to perform independent initialization for the class and for categories of the class, you should implement load methods.

    我们现在根据文档看看initialize是什么含义。
    1.1initialize什么时候会被调用,根据加粗的第一句话,我们可以知道runtime会在身或者其子类在被第一次接收到消息(allocWithZone)的时候,initialize就会被发送给我们。当其子类被调用的时候父类的也会被调用,这时候如果生成一个父类,那么父类的initialize就不会被调用,那是因为父类已经被调用过了,但是如果你要alloc一个父类的另一个子类,并且这个子类没有重写initialize那么意味着父类也会被调用一次。也就是说子类的initialize是在整个程序中只会调用一次,但是父类可能会被调用多次。这也就是为什么父类中initialize会被加入一个IF
    1.2为什么要加入这个IF呢?self == [ClassName self]。因为父类的initialize可能会被调用多次,那么加入这一话是为了保护我们自己制定的类要调用的code。我感觉我理解的不深刻,如果那位大神知道请指点。
    1.3第三部分加粗,翻译成如果想让分类和本类的initialize都会被调用那么你必须在每个分类和本类中重写load方法。关于分类我在后面给大家讲解

    (2)用途
    作用就是可以在initialze方法中初始化一个写static变量,我们知道如果你在一个.m文件中给一个static变量赋值这是不允许的,编译会报错。

    3.initialize在分类中的反应
    有一个Teacher类,

    +(void)initialize {
    NSLog(@"%s",__func__);
    }

    Teacher有两个分类分别是

    @implementationTearch (Category)
    + (void)initialize {
    NSLog(@"category");
    }
    @end

    @implementationTearch (Zhao)
    + (void)initialize {
    NSLog(@"Zhao");
    }
    @end

    当对Teacher进行alloc的时候,控制台打印出来的是

    2016-08-30 22:24:01.803 load & initialize[23691:1040682] Zhao

    可以看出来只能运行一个initilaze,那么为什么有两个问题(1)本类和分类比先运行分类吗?(2)两个分类为什么先运行(Zhao)呢?有什么依据吗?
    解答上面的问题:首先说说我们如何来测试这个问题,就是我们在上面compile source中来调用Tearch 和Tearch 分类的先后顺序。我们得到了结论:
    分类的优先级要比本类高,有分类执行分类
    分类之间的优先级根据compile source 中那个分类在上面反而没有被执行。
    (3)根据文档来看看重写load方法让他们都可以每一个initialzie都运行。但是我给他们都加上load方法依然执行了分类的initialize。希望大神指点。当然在后面的研究里面如果我知道我也会更新我的文章希望大家谅解

    4.load在分类中
    每一个本类和分类都运行load方法

    5.总结
    5.1load方法是在main方法执行之前,initialize是在main方法之后,由此我们可以知道load方法中没有什么autorelease runloop。
    5.2load方法适合做一些方法实现的替换。不适合生成一些变量。做很复杂的事情
    5.3initialize方法适合进行一写static变量的初始化

    相关文章

      网友评论

      本文标题:Load 和 initialize 的研究

      本文链接:https://www.haomeiwen.com/subject/vxwadttx.html