0

我想延迟从 Today Extension 中加载的框架内方法的执行。

我试过这个:

接收任务和等待时间的函数

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))

但是这个电话挂断了应用程序,使其在超过那个时间并且直到永远没有响应。

4

1 回答 1

1

dispatch_time需要以纳秒为单位的时间增量,而不是以毫秒为单位,因此您的函数很可能确实有效,只是相差了 1000000 倍。试试:

let popTime = dispatch_time(DISPATCH_TIME_NOW, delayInMilliseconds * NSEC_PER_MSEC)
于 2015-02-09T20:11:57.683 回答