我有一个计时器:
let queue = DispatchQueue(label: "com.domain.app.timer")
timer = DispatchSource.makeTimerSource(queue: queue)
timer.schedule(deadline: .now(), repeating: 30.0, leeway: .seconds(0))
...
timer.resume()
除一件事外,它工作完美。计时器在我的 timer.resume() 之后执行第一次迭代。但是我需要在重复时间后执行第一次迭代(在我的例子中是 30 秒)。如何做到这一点?
最佳答案
deadline
参数决定首次投递时间(主题
计时器合并、余地、...) 和 repeating
参数
确定第一次交付后的间隔。
因此
timer.schedule(deadline: .now() + 30.0, repeating: 30.0, leeway: .seconds(0))
安排定时器在 30 秒后发送,然后每隔 30 秒。
关于swift - DispatchSourceTimer 重复时间后如何执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49190656/