gpt4 book ai didi

swift - DispatchQueue.main.asyncAfter 不准确

转载 作者:搜寻专家 更新时间:2023-10-30 21:51:22 26 4
gpt4 key购买 nike

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

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

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 调用

最佳答案

asyncAfter 仅保证至少等待指定的时间。

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

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

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

关于swift - DispatchQueue.main.asyncAfter 不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40995461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com