这是我第一次使用 GCD,我承认,如果我犯了傻,我深表歉意。我有一个 dispatch_after 命令,它对我来说是一个方便的延迟。
我的问题是当我发送
dispatch_after(500000000000, dispatch_get_main_queue()){
println("triggered") //or any other code
}
关闭立即触发(例如,我已经测试过这个并立即“触发”打印)。应该需要更长时间吧?大概多 500 秒。
谢谢:)
最佳答案
dispatch_after(_:_:_:)
的第一个参数不是延时,而是时间点。来自 the docs :
when: The temporal milestone returned by
dispatch_time
ordispatch_walltime
.Discussion This function waits until the specified time and then asynchronously adds block to the specified queue.
你需要构造一个相对于当前时间的延迟,使用dispatch_time(_:_:)
:
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(500 * NSEC_PER_SEC))
dispatch_after(delayTime, dispatch_get_main_queue()) { ... }
关于swift - dispatch_after 时间立即触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31949034/