DispatchQueue.main.asyncAfter不准确

时间:2016-12-06 12:28:57

标签: swift delay

我试图在延迟后调用一个函数。在iOS10上,我可以使用Timer.scheduledTimer,它确实在给定的延迟后调用我的闭包。但是,在iOS9上我使用的是DispatchQueue.main.asyncAfter,它以6秒的延迟调用我的闭包。

我正在测试的延迟是60秒。 Timer.scheduledTimer在60秒后调用闭包,DispatchQueue.main.async在66秒后调用。六秒延迟是结果,如果我安排两次延迟60秒,则使用DispatchQueue.main.asyncAfter在132秒后调用第二个延迟。

func delay(delay:Double, closure:@escaping ()->()) {
    NSLog("Calling method with delay of: \(delay)")

    if #available(iOS 10.0, *) {
        Timer.scheduledTimer(withTimeInterval: delay, repeats: false) { (timer) in
            closure()
        }
    } else {
        // TODO: Is not accurate, delay of +- 6 seconds
        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
            closure()
        }
    }
}

调用延迟函数的代码:

func scheduleStudy(minutes: Int, secondsFromNow: Int) {
    // Study notification
    if #available(iOS 10.0, *) {
        self.addiOS10StudyNotification(minutes: minutes, secondsFromNow: secondsFromNow)
    }else{
        self.addiOS9StudyNotification(minutes: minutes, secondsFromNow: secondsFromNow)
    }

    // Study timer
    delay(delay: Double(secondsFromNow)) {
        self.onStudy(minutes: minutes)
    }

    NSLog("Scheduled study for \(minutes) minutes in \(secondsFromNow) seconds from now.")
}

使用Timer.scheduledTimer规划通知和方法调用

2016-12-06 13:34:06.024714 Mattie[1386:360881] Calling method with delay of: 0.0
2016-12-06 13:34:06.025072 Mattie[1386:360881] Scheduled study for 1 minutes in 0 seconds from now.
2016-12-06 13:34:06.036953 Mattie[1386:360881] Calling method with delay of: 60.0
2016-12-06 13:34:06.037191 Mattie[1386:360881] Scheduled pause for 1 minutes in 60 seconds from now.
2016-12-06 13:34:06.052520 Mattie[1386:360881] Calling method with delay of: 120.0
2016-12-06 13:34:06.053162 Mattie[1386:360881] Scheduled study for 1 minutes in 120 seconds from now.
2016-12-06 13:34:06.066838 Mattie[1386:360881] Calling method with delay of: 180.0
2016-12-06 13:34:06.067027 Mattie[1386:360881] Scheduled finish in 180 seconds from now.

暂停:

2016-12-06 13:35:06.038307 Mattie[1386:360881] ON PAUSE
2016-12-06 13:35:06.065389 Mattie[1386:360881] Added pause timer for 1 minutes

计划于13:34:06,在13:35:06调用

使用DispatchQueue.main.asyncAfter

规划通知和方法调用
2016-12-06 13:36:48.845838 Mattie[1390:361681] Calling method with delay of: 0.0
2016-12-06 13:36:48.847389 Mattie[1390:361681] Scheduled study for 1 minutes in 0 seconds from now.
2016-12-06 13:36:48.854336 Mattie[1390:361681] Calling method with delay of: 60.0
2016-12-06 13:36:48.854543 Mattie[1390:361681] Scheduled pause for 1 minutes in 60 seconds from now.
2016-12-06 13:36:48.861424 Mattie[1390:361681] Calling method with delay of: 120.0
2016-12-06 13:36:48.861601 Mattie[1390:361681] Scheduled study for 1 minutes in 120 seconds from now.
2016-12-06 13:36:48.868464 Mattie[1390:361681] Calling method with delay of: 180.0
2016-12-06 13:36:48.868644 Mattie[1390:361681] Scheduled finish in 180 seconds from now.

暂停:

2016-12-06 13:37:54.865400 Mattie[1390:361681] ON PAUSE
2016-12-06 13:37:54.897354 Mattie[1390:361681] Added pause timer for 1 minutes

计划于13:36:48,在13:37:54调用

1 个答案:

答案 0 :(得分:13)

asyncAfter只能保证至少等到指定的时间。

如果你想在iOS9上准确的时间间隔,你可能仍然应该使用Timer。

Timer.scheduledTimer(timeInterval: delay, 
               target: self, 
             selector: #selector(executeClosure), 
             userInfo: nil, 
              repeats: false)

executeClosure是一个执行最后保存的闭包的函数。