Search code examples
iosswiftios8ios8-today-widgettoday-extension

It is possible to delay the execution of a function on Today Extensions (iOS)


I want to delay the execution of a method inside a framework that gets loaded from within a Today Extension.

I have tried this:

A function that receives the task and the time to wait

func playCPUWithDelay(delayInMilliSeconds:Int64,scheduledTask: ()->Void)
 {

    let popTime = dispatch_time(DISPATCH_TIME_NOW,delayInMilliSeconds) // 1
    dispatch_after(popTime, GlobalMainQueue) { // 2

    scheduledTask()

}

being

var GlobalMainQueue: dispatch_queue_t {
return dispatch_get_main_queue()
}

but it gets runned without delay

also have also tried..

NSThread.sleepForTimeInterval(NSTimeInterval(5000))

but this call hangs up the app, making it unresponsive for more than that time and until forever.


Solution

  • dispatch_time takes a time delta in nanoseconds, not in milliseconds, so it's likely your function actually works, it's just off by a factor of 1000000. Try:

    let popTime = dispatch_time(DISPATCH_TIME_NOW, delayInMilliseconds * NSEC_PER_MSEC)