Simple and Elegant Timer
中文介绍:打造一个优雅的Timer
- No retain cycle
- Decouple with RunLoop
- Support GCD queue
- Support dynamically changing interval
- Support closure syntax
//We need to maintain a strong reference to SwiftTimer. When self dealloc, timer will dealloc too.
self.timer = SwiftTimer(interval: .seconds(2)) {
print("fire")
}
self.timer.start()timer = SwiftTimer.repeaticTimer(interval: .seconds(1)) {
print("fire")
}
timer.start()dynamically changing interval
timer = SwiftTimer.repeaticTimer(interval: .seconds(5)) { timer in
print("doSomething")
}
timer.start() // print doSomething every 5 seconds
func speedUp(timer: SwiftTimer) {
timer.rescheduleRepeating(interval: .seconds(1))
}
speedUp(timer) // print doSomething every 1 second The closure will be called after interval you specified. It is invalid to call again in the interval.
SwiftTimer.throttle(interval: .seconds(0.5), identifier: "refresh") {
refresh();
}The closure will be called after interval you specified. Calling again in the interval cancels the previous call.
SwiftTimer.debounce(interval: .seconds(0.5), identifier: "search") {
search(inputText)
}timer = SwiftCountDownTimer(interval: .fromSeconds(0.1), times: 10) { timer , leftTimes in
label.text = "\(leftTimes)"
}
timer.start()SwiftTimer can be added as a Swift Package Manager dependency with Swift 5.0 or later.
The package exposes the SwiftTimer library and keeps the existing deployment targets:
iOS 8, macOS 10.11, tvOS 10, and watchOS 3. Your Xcode version may require a newer
deployment target. The package also builds on Linux using Foundation and Dispatch.
In Xcode, choose File > Add Package Dependencies, enter
https://github.com/100mango/SwiftTimer.git, and add the SwiftTimer product to your target.
Select Up to Next Major Version starting at 1.2.0. Version 1.2.0 is the
first release with a package manifest; the older v1.0 and 1.1 tags do not
support Swift Package Manager.
For a Package.swift dependency:
dependencies: [
.package(url: "https://github.com/100mango/SwiftTimer.git", from: "1.2.0")
],
targets: [
.target(name: "YourTarget", dependencies: ["SwiftTimer"])
]Then use import SwiftTimer in your Swift files. The package compiles in Swift 5
language mode, including when used with a Swift 6 toolchain.
Use each timer instance from a single serial queue. fire() calls its handler
synchronously on the caller's queue; scheduled events use the queue passed to the
initializer. Keep a strong reference to a timer for as long as it is needed.
Debounce and throttle registration and cancellation are safe to call concurrently. Identifiers share one registry across both operations. A pending throttle ignores the same identifier; debounce replaces any pending work with that identifier. The identifier becomes available before its handler runs, so handlers may schedule or cancel new work with the same identifier. Cancellation prevents pending work from starting; it does not interrupt a handler that has already started.
github "100mango/SwiftTimer"
Run the cross-platform package tests with a current Swift toolchain:
swift test
swift test -c releaseThe package tests cover timers, countdowns, debounce, and throttle without UIKit, including lifecycle, reentrant handlers, concurrent cancellation, and input boundaries. The existing Xcode project and its iOS tests remain available for Apple-platform validation; Linux package tests do not replace Apple SDK or device testing.
