我想延迟从 Today 扩展中加载的框架内方法的执行。
我试过这个:
接收任务和等待时间的函数
func playCPUWithDelay(delayInMilliSeconds:Int64,scheduledTask: ()->Void)
{
let popTime = dispatch_time(DISPATCH_TIME_NOW,delayInMilliSeconds) // 1
dispatch_after(popTime, GlobalMainQueue) { // 2
scheduledTask()
}
存在
var GlobalMainQueue: dispatch_queue_t {
return dispatch_get_main_queue()
}
但它会立即运行
也试过..
NSThread.sleepForTimeInterval(NSTimeInterval(5000))
但是此调用会挂断应用程序,使其在超过该时间的时间内无响应,直至永远。
最佳答案
dispatch_time
需要以纳秒为单位的时间增量,而不是以毫秒为单位,因此您的函数很可能确实有效,只是相差了 1000000 倍。尝试:
let popTime = dispatch_time(DISPATCH_TIME_NOW, delayInMilliseconds * NSEC_PER_MSEC)
关于ios - 可以延迟 Today Extensions (iOS) 上的功能执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28418383/