swift - DispatchSourceTimer 重复时间后如何执行

标签 swift

我有一个计时器:

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/

相关文章:

ios - 制作一个应用程序,我想让照片可选,但在制作新帖子时需要标题

xcode - 使用 contentsOfFile 的 UIImage

swift - Xcode 永远持续加载项目

ios - 从 UITableViewCell 到 UIViewController 的 segue 不起作用(准备不调用 segue)

Swift 核心数据 : auto-generating managed object subclass makes the class name to PRODUCT_MODULE_NAME. 模型中的实体名称

arrays - Swift 5 将文本文件读入 Int/Doubles 的二维数组

ios - SwiftUI WKWebView 检测 url 更改

swift - 未触发 NSToolbarItem 的操作

ios - "Fatal error: unexpectedly found nil while unwrapping an Optional value"- 将图像发布到 FaceBook

swift - 我们如何从 Apple TV App 打开 Apple TV 商店