Intuitive Swift timers with automatic memory management
Medium article: Simplifying Swift Timers: solving memory leaks & complexity once and for all
Timers is a lightweight package that provides a convenient way to handle and manage timers in Swift, especially in view controllers to automatically manage lifetime of timers.
import Timers
final class RefreshingViewController: UIViewController {
let timers = Timers()
func viewDidLoad() {
super.viewDidLoad()
timers.addRepeating(timeInterval: 1.0, withTarget: self) { (self) in
self.reloadData()
}
}
func reloadData() {
// reload your data here
}
}
Timers takes care of invalidating the timers when the view controller is deinitialized, eliminating the need to do it manually.
Note
Help save Ukraine. Donate via United24, the official fundraising platform by the President of Ukraine
- Automatically manages timers lifetime: no need to worry about memory leaks
- Repeated timers
- One-off timers
- Timers that fire at a specific date and then repeat with a set interval
- Full access to
Foundation.Timer
APIs for more complex use cases - Easy to extend
- Unit-tested
- Click File → Swift Packages → Add Package Dependency.
- Enter
https://github.com/dreymonde/Timers.git
Note
Timers is a very simple library and only consists of one file (Timers.swift). Do not expect updates, this release is likely final. If you need additional functionality, feel free to fork or copy Timers.swift directly into your project and extend. PRs are also welcome.
All these timers are managed by the
Timers
instance and are invalidated automatically when theTimers
instance is deallocated.
import Timers
final class MyViewController: UIViewController {
let timers = Timers()
override func viewDidLoad() {
super.viewDidLoad()
timers.addRepeating(timeInterval: 1.0, withTarget: self) { (self, timer) in
self.reloadData()
}
}
func reloadData() {
// reload your data here
}
}
timers.addRepeating(timeInterval: 1.0, tolerance: 0.1, withTarget: self) { (self) in
self.reloadData()
}
In this case, the timer will have a slight tolerance of 0.1 seconds which allows the system to adjust the firing of the timer for better system performance.
let date = Date().addingTimeInterval(5) // Date 5 seconds from now
timers.fireAt(date, withTarget: self) { (self) in
self.reloadData()
}
// or:
timers.fireAfter(timeInterval: 5, withTarget: self) { (self) in
self.reloadData()
}
timers.addRepeating(
initiallyFireAt: Date().addingTimeInterval(10),
thenRepeatWithInterval: 5.0,
withTarget: self
) { (self, timer) in
self.reloadData()
}
For a more custom use case where you want to add the timer manually:
let customTimer = Timer(timeInterval: 1.0, repeats: true) { _ in
print("This is a custom timer")
}
timers.addTimerManually(timer: customTimer)
At any point if you want to stop and invalidate all timers you can call:
timers.clear()
- Time by @dreymonde - Type-safe time calculations in Swift, powered by generics
- DateBuilder by @dreymonde - Create dates and date components easily, of any complexity (e.g. "first Thursday of the next month")