Gatling 是一个用 Swift 编写的计时库,它让您创建多台计时器,多线程。每个计时器都有自己的间隔、工作队列和其他配置。
Gatling.loadWithTarget(self, timeInterval: 2.0)
通常,Gatling 通过调用“回调”方法来通知调用者计时器正在触发(Gatling 正在射击)。您只需简单地实现该方法以接收此事件。
extension MyClass: GatlingTarget {
func shotWithBullet(bullet: Bullet?) {
print("Ahhhh, I'm being shot by gatling")
}
}
除了上面提到的最简单的计时器之外,您还可以为您的计时器进行更多详细配置。包括
shouldShootImmediately: Bool
:指示计时器是否应立即触发。如果为 false
则不会,就像 NSTimer
一样;如果为 true
,则计时器在调用 '加载' 方法后将立即执行额外的触发。workingQueue: dispatch_queue_t
:回调将在哪个任务队列中执行。是的,Gatling 是一个多线程计时器,因此您可以选择任何队列。bullet: Bullet?
:计时器的用户信息,将其视为 NSTimer
的 userInfo
。Gatling 将将其在回调方法中返回给您。 Gatling.loadWithTarget(self, timeInterval: 1.5) { (configuration) in
configuration.shouldShootImmediately = true
configuration.workingQueue = dispatch_queue_create("com.mycompany.queue.working", nil)
configuration.bullet = ["Identifier": "some identifier"]
}
您可以使用回调闭包接收计时器的触发事件。在 Configuraion
中指定它。
Gatling.loadWithTarget(self, timeInterval: 1.5) { (configuration) in
configuration.onShoot = { bullet in
print("Ahhhh, I'm being shot by gatling")
}
}
注意:如果您指定了回调闭包,则不会调用“回调方法”。
Gatling旨在为大多数 Cocoa 开发者提供使用多个计时器的简单方式。因此,它**不是一个高精度计时器**。
Gatling使用 GCD 作为底层技术。因此,最好保持计时器的工作队列简洁简单,以避免阻塞队列。最佳实践是为每个单独的计时器使用独立的队列。
在尽可能的情况下,Gatling使用 mach 绝对时间来提供最高的精度。有关更多信息,请参阅https://developer.apple.com/library/mac/qa/qa1398/_index.html
有关高精度计时器的更多信息,或您确实需要一个高精度计时器,请参阅https://developer.apple.com/library/ios/technotes/tn2169/_index.html
Gatling 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中:
pod "Gatling"
Gatling 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。