performSelector 详解 —— 从 What 到 When

缘起

我最近被一位面试官问:

  • performSelector 是我们常用的方法,能不能从 Runloop 的角度说说?
  • performSelector 跟我们直接调用一个方法有什么区别?

一下子就把我搞蒙了,连 performSelector 都没用过,最多在项目代码里读到,只知道大概是干嘛的,现在马上来搞懂它!

performSelector 是我们常用的方法,能不能从 Runloop 的角度说说?

这个方法的入参是一个 selector,selector 属于 SEL 类型,用来代表一个函数,实际上是一个 C 的 string。打印一个 selector 会得到对应方法的名字。详见官方文档。

这里他要问的其实是 performSelector:withObject:afterDelay 方法(Apple 文档在这里),只有这个才跟 Runloop 有关系。这个方法会生成一个定时器 Timer,并把它添加到当前线程的 Runloop 上,到了预定时间后 Runloop 好去执行对应的 selector (即执行方法)。

objc 复制代码
+ (void)performSelectorInMainThread {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self performSelector:@selector(test) withObject:nil afterDelay:1.0];
    });
}

+ (void)test {
    NSLog(@"This is test method");
}

默认是 NSDefaultRunLoopMode,当传入的时间间隔到的时候,就检查当前 Runloop 是否是那个 mode,如果是就会成功执行。如果不是,那么就要等一等,等 Runloop 的 mode 匹配。

注意:如果是在子线程 中调用 performSelector:withObject:afterDelay 方法,那就得手动获取一下 runloop,同时得让子线程中的 Runloop 处于 run 状态。因为子线程中的 Runloop 不是默认创建,更不会默认 run 起来。

objc 复制代码
+ (void)performSelectorInOtherThread {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"1");
        NSRunLoop *runloop = [NSRunLoop currentRunLoop]; // 注释这句,则不会执行 test 方法
        [self performSelector:@selector(test) withObject:nil afterDelay:1.0];
        [runloop run];// 注释这句,也不会执行 test 方法; 
        //同时,这句需要放在 performSelector 后面,对应的 timer 才会被添加到 Runloop 中
        NSLog(@"3");
    });
}

+ (void)test {
    NSLog(@"This is test method");
}

这里其实也可以引申两个问题:

  1. 如何查看 Runloop 拥有的「源」?
  2. Runloop 在什么时候会退出?(在本示例代码中,"3" 是在 Runloop 退出的时候打印的,那 Runloop 怎么为啥会退出?)
    想了解详情,请看 Runloop 与 performSelector

performSelector 跟我们直接调用一个方法有什么区别?

performSelector 是运行时决议的,意味着用 performSelector 可以到运行时才决定要执行哪个方法,因此编译器不会检查 performSelector 的入参所代表的方法 是否已经被 caller 对象实现。(因此,这时你可以在「不 import 头文件」的情况下调用方法)

而对于一般的方法调用,编译器会严格把控当前 caller 是否实现了该方法,如果没有,根本过不了编译。

这就有点类似于 KVC(Key-Value-Coding),用一个字符串去调用一个函数。

performSelector 的使用

因为对应的 selector 有可能实现,也可能没实现,所以它常常跟 respondsToSelector: 搭配使用,如下代码:

objc 复制代码
// 检查 self 对象是否实现了 test 方法
if([self respondsToSelector:@selector(test)]) {
    [self performSelector:@selector(test)];
}

另外,根据 Apple 的文档描述,performSelector 可能会带来内存管理问题:

But use caution when doing this. Different messages require different memory management strategies for their returned objects, and it might not be obvious which to use.

Usually the caller isn't responsible for the memory of a returned object, but that's not true when the selector is one of the creation methods, such as copy. See Memory Management Policy in Advanced Memory Management Programming Guide for a description of ownership expectations. Depending on the structure of your code, it might not be clear which kind of selector you are using for any given invocation.

Due to this uncertainty, the compiler generates a warning if you supply a variable selector while using ARC to manage memory. Because it can't determine ownership of the returned object at compile-time, ARC makes the assumption that the caller does not need to take ownership, but this may not be true. The compiler warning alerts you to the potential for a memory leak.

大意是,如果提供的 selector 是构造方法,那么可能会带来所有权的不确定,进一步导致内存泄漏 的问题。如果确定 selector 不会返回值,建议改用 performSelectorOnMainThread:withObject:waitUntilDone: 。

也可以改用 NSInvokation,构建一个可以返回值的 message(selector 可以被看做是一个 message,因为 Objective-C 调用方法的方式是「消息转发」)

performSelector 的使用场景

动态化和组件化

多线程

objc 复制代码
// 主线程
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array;

// 子线程
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array

延后执行

objectivec 复制代码
[self performSelector:@selector(test) withObject:nil afterDelay:1.0];

参考资料

相关推荐
Sam_Deep_Thinking2 小时前
什么是CountDownLatch?
java·后端·面试·程序员
雾喔3 小时前
近日总结 & 面试经验
面试·职场和发展
bksczm3 小时前
0基础面试2
网络·面试·职场和发展
keke.shengfengpolang4 小时前
2026年People Analytics校招技术能力拆解:SQL取数、Power BI看板、NLP文本分析,面试到底考什么
面试
顶点多余5 小时前
9.24 面试复盘
面试·职场和发展
炎龙铠甲会合体5 小时前
AXI2ICACHE— AXI4-Full 四路组相联指令 Cache 设计与 UVM 验证
fpga开发·面试·芯片验证
怕浪猫6 小时前
个人开发者、OPC 个体狂喜的免费资源网站合集,额度不是免费试用:1300个开发者资源
面试·架构·github
此时不提桶,更待何时20 小时前
05-03-B-ClickHouse与OLAP面试与生产事故实战
clickhouse·面试·nosql
此时不提桶,更待何时21 小时前
03-04-B-连接池与DB治理面试与生产事故实战
mysql·面试
kaiyou20261 天前
数据分析岗面试,如何把考证学到的知识讲成业务案例?
面试·数据挖掘·数据分析