performSelector:withObject:afterDelay: 在子线程中调用不执行

官网文档

Discussion This method sets up a timer to perform the aSelector message on the current thread’s run loop. The timer is configured to run in the default mode (NSDefaultRunLoopMode). When the timer fires, the thread attempts to dequeue the message from the run loop and perform the selector. It succeeds if the run loop is running and in the default mode; otherwise, the timer waits until the run loop is in the default mode.
If you want the message to be dequeued when the run loop is in a mode other than the default mode, use the performSelector:withObject:afterDelay:inModes: method instead. If you are not sure whether the current thread is the main thread, you can use the performSelectorOnMainThread:withObject:waitUntilDone: or performSelectorOnMainThread:withObject:waitUntilDone:modes: method to guarantee that your selector executes on the main thread. To cancel a queued message, use the cancelPreviousPerformRequestsWithTarget: or cancelPreviousPerformRequestsWithTarget:selector:object: method.

讨论 此方法设置一个计时器,以在当前线程的运行循环上执行aSelector消息。计时器配置为以默认模式(NSDefaultRunLoopMode)运行。当计时器触发时,线程会尝试将消息从运行循环中出列并执行选择器。如果运行循环正在运行且处于默认模式,它会成功;否则,计时器等待直到运行循环处于默认模式。
如果希望在运行循环处于默认模式以外的模式时将消息出列,请使用performSelector:withObject:afterDelay:inModes:方法。如果您不确定当前线程是否是主线程,则可以使用performSelectorOnMainThread:withObject:waitUntilDone:或performSelectorOnMainThread:withObject:waitUntilDone:modes:方法来保证您的选择器在主线程上执行。要取消排队的消息,请使用cancelPreviousPerformRequestsWithTarget:或cancelPreviousPerformRequestsWithTarget:selector:object:方法。

Special Considerations This method registers with the runloop of its current context, and depends on that runloop being run on a regular basis to perform correctly. One common context where you might call this method and end up registering with a runloop that is not automatically run on a regular basis is when being invoked by a dispatch queue. If you need this type of functionality when running on a dispatch queue, you should use dispatch_after and related methods to get the behavior you want.

特别注意事项 此方法向其当前上下文的runloop注册,并依赖于定期运行的runloop以正确执行。 您可以调用此方法并最终使用不会自动定期运行的runloop注册的一个常见上下文是由调度队列调用。 如果在调度队列上运行时需要此类功能,则应使用dispatch_after和相关方法来获取所需的行为。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值