dispatch_after时间立即触发 [英] dispatch_after time triggers immediately
问题描述
我承认,这是我第一次使用GCD,很抱歉,如果我很愚蠢.我有一条dispatch_after命令,对我来说这是一个方便的延迟.
This is the first time I've used GCD, I'll admit, so sorry if I've been stupid. I have a dispatch_after command which acts as a handy delay for me.
我的问题是,我发送邮件时
My problem is that when I send
dispatch_after(500000000000, dispatch_get_main_queue()){
println("triggered") //or any other code
}
关闭立即触发(例如,我已经对此进行了测试,并立即触发"了打印).应该花更长的时间吧?大概再长500秒.
the closure is triggered immediately (e.g. I have tested this and "triggered" prints immediately). It should take longer right? Like 500 seconds longer.
谢谢:)
推荐答案
dispatch_after(_:_:_:)
的第一个参数不是延迟,而是时间点.来自文档:
The first parameter of dispatch_after(_:_:_:)
is not a delay, but a point in time. From the docs:
何时:
dispatch_time
或dispatch_walltime
返回的时间里程碑.
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(_:_:)
构造相对于当前时间的延迟:
You need to construct a delay relative to the current time, using dispatch_time(_:_:)
:
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(500 * NSEC_PER_SEC))
dispatch_after(delayTime, dispatch_get_main_queue()) { ... }
这篇关于dispatch_after时间立即触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!