dispatch_after 会阻塞主线程吗?

Does dispatch_after block the main thread?

我正在设置一个计时器,以便在一秒钟后为我的键盘扩展重置一个值。问题是我觉得下面的调用拖延了我的 UI:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    [self resetDoubleTapBool];
})

有没有一种异步的方式来做到这一点,或者一般来说更好的方式?谢谢!

根据 function documentation:

This function waits until the specified time and then asynchronously adds block to the specified queue.

dispatch_after() 调用本身不会阻塞。在指定的时间(或之后不久),块将被提交到主队列。提交它不会阻塞主线程。当主线程下一个 运行 进入其 运行 循环或在 dispatch_main() 内空闲时,它将执行该块。

IF 你的 -resetDoubleTapBool 方法需要相当长的时间,这可能会拖延你的 UI。这与主线程上 运行 的任何代码都一样。它不特定于 dispatch_after() 或 GCD 的任何其他部分。