Suzaku
Suzaku 是哈希轮定时器的 Swift 版本。
特性
- 线程安全
- GCD 定时器
- 局部变量定时器
示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
用法
哈希轮定时器用作内核和网络堆栈的基础。适用于大量定时任务并发的情况。Suzaku 是为 iOS 客户端设计的哈希轮定时器的 Swift 实现,适用于直播房间和持久连接等情况。
/// normal
class SomeClass {
let timer = try! HashedWheelTimer(tickDuration: .seconds(1), ticksPerWheel: 8, dispatchQueue: nil)
var counter = 0
let dateFormatter = DateFormatter()
func someFunction() {
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
timer.resume()
_ = try? timer.addTimeout(timeInterval: .seconds(3), reapting: true) { [weak self] in
guard let self = self else { return }
self.counter += 1
print("counter: \(self.counter), \(self.dateFormatter.string(from: Date()))")
}
}
}
/// local variable timer
class SomeClass {
let dateFormatter = DateFormatter()
func someFunction() {
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
var localTimer: HashedWheelTimer? = try! HashedWheelTimer(tickDuration: .seconds(1), ticksPerWheel: 1, dispatchQueue: nil)
localTimer?.resume()
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
_ = try? localTimer?.addTimeout(timeInterval: .seconds(5), reapting: true) { [weak self] in
guard let self = self else { return }
print("fired \(self.dateFormatter.string(from: Date()))")
localTimer?.stop()
localTimer = nil
}
}
}
}
需求
- iOS 10.0+
- Swift 5.0+
- Xcode 11+
- macOS 10.12+
安装
CocoaPods
Suzaku 可以通过 CocoaPods 获取。安装它,只需将以下行添加到您的 Podfile:
pod 'Suzaku'
Swift 包管理器
Swift 包管理器 是一个自动化分发 Swift 代码的工具,并集成到 swift
编译器中。它还处于早期开发阶段,但 Alamofire 支持在支持的平台中使用它。
一旦设置了您的 Swift 包,将 Alamofire 添加为依赖项就像将其添加到您的 Package.swift
中的 dependencies
值一样简单。
dependencies: [
.package(url: "https://github.com/elijahdou/Suzaku.git")
]
作者
elijahdou, [email protected]
许可
Suzaku 在 Apache 2.0 许可下提供。有关更多信息,请参阅 LICENSE 文件。