Does dispatch_after block the main thread?

穿精又带淫゛_ 提交于 2019-12-07 14:26:08

问题


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!


回答1:


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.

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.




回答2:


According to the function documentation:

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



来源:https://stackoverflow.com/questions/28382990/does-dispatch-after-block-the-main-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!