运行 循环的同步和异步输入源有什么区别?

what is the difference between sync and async input source for run loops?

端口等输入源向 运行 循环异步传送事件而计时器同步传送事件是什么意思。

定时器是否阻塞线程?

Threading Programming Guide: Run Loops 说:

A run loop receives events from two different types of sources. Input sources deliver asynchronous events, usually messages from another thread or from a different application. Timer sources deliver synchronous events, occurring at a scheduled time or repeating interval. Both types of source use an application-specific handler routine to process the event when it arrives.

但是计时器只会在计时器的闭包或选择器方法 运行ning 时阻塞线程。但是一旦你 return 从那里,线程就不再被阻塞。所以一定要尽快进出。[​​=14=]

例如,如果您已安排定时器在 10 秒内触发,而定时器处理 closure/selector 中的代码需要 100 毫秒到 运行,则线程不会被阻塞,直到计时器触发,然后仅持续 100 毫秒。与重复计时器相同。

最重要的是,只要您没有在计时器处理程序中做任何计算量太大的事情,就没有什么可担心的。如果你确实需要做任何可能阻塞任何 material 时间量的事情,那么要么让你的定时器处理程序异步地将相关代码分派到某个后台队列,要么只安排一个 GCD 定时器到 运行直接在后台队列上,完全绕过 Timer

但对于大多数 Timer 用例,这不是问题。