iOS开发之 runtime(37) :load 方法调用
2019-08-11 本文已影响10人
kyson老师
![](https://img.haomeiwen.com/i1672498/0e5fbbe4f6f3d523.png)
本系列博客是本人的源码阅读笔记,如果有 iOS 开发者在看 runtime 的,欢迎大家多多交流。为了方便讨论,本人新建了一个微信群(iOS技术讨论群),想要加入的,请添加本人微信:zhujinhui207407,【加我前请备注:ios 】,本人博客http://www.kyson.cn 也在不停的更新中,欢迎一起讨论
本文完整版详见笔者小专栏:https://xiaozhuanlan.com/runtime
前言
上一篇文章笔者讲解了 load 方法的准备工作,主要是将拥有 load 方法的类或者分类找出来,并将其放入到静态变量数组 loadable_classes 以及 loadable_categories 中,并在文章中给出猜测 load 方法调用的顺序。本文将给大家介绍 load 方法的调用过程。
分析
call load 方法代码如下:
void call_load_methods(void)
{
static bool loading = NO;
bool more_categories;
loadMethodLock.assertLocked();
// Re-entrant calls do nothing; the outermost call will finish the job.
if (loading) return;
loading = YES;
void *pool = objc_autoreleasePoolPush();
do {
// 1. Repeatedly call class +loads until there aren't any more
while (loadable_classes_used > 0) {
call_class_loads();
}
// 2. Call category +loads ONCE
more_categories = call_category_loads();
// 3. Run more +loads if there are classes OR more untried categories
} while (loadable_classes_used > 0 || more_categories);
objc_autoreleasePoolPop(pool);
loading = NO;
}