dispatch_after是否会阻塞主线程? [英] Does dispatch_after block the main thread?

查看:611
本文介绍了dispatch_after是否会阻塞主线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置计时器,以便在经过一秒钟后重置键盘扩展名的值.问题是我感觉以下调用使我的UI停滞了:

I'm setting a timer so that after a second passes I reset a value for my keyboard extension. The problem is that I feel like the following call is stalling my UI:

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

是否有异步的方法或一般更好的方法?谢谢!

Is there an asynchronous way of doing this, or a better way in general? Thanks!

推荐答案

dispatch_after()调用本身不会阻塞.在指定时间(或之后不久),该区块将被提交到主队列.提交它不会阻塞主线程.当主线程下一次运行其运行循环或在dispatch_main()中处于空闲状态时,它将执行该块.

The dispatch_after() call itself does not block. At (or shortly after) the appointed time, the block will be submitted to the main queue. Submitting it doesn't block the main thread. When the main thread next runs its run loop or is idle within dispatch_main(), it will execute the block.

如果,您的-resetDoubleTapBool方法需要花费大量时间,这可能会导致UI停滞.这与在主线程上运行的任何代码相同.它不特定于dispatch_after()或GCD的任何其他部分.

IF your -resetDoubleTapBool method takes any appreciable amount of time, that can stall your UI. That's just the same as any code that runs on the main thread. It's not specific to dispatch_after() or any other part of GCD.

这篇关于dispatch_after是否会阻塞主线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆