A simple swift GCD based Timer
Why do we need a new timer if we already have NSTimer
?
NSTimer
is an Objective-C class that needs a @selector
to call. As in swift, we don't have selectors, whe have to pass a String
with the name of the function we want to be called. Something like :
let timer = NSTimer(timeInterval: 0.5, target: self, selector: "myfunction", userInfo: nil, repeats: false)
Yikes!
The problem with that is that myfunction
string will be evaluated in run-time. That means we would make a typo in the name of the function and the compiler won't be able to tell us that code won't execute.
Wouldn't it be nice if whe could pass a closure to the timer?
Using PDKTimer
is really easy. Simply call
let timer = PDKTimer(timeInterval: 5.0, repeats: true){
// do something
}
and that closure will be executed each 5 seconds
PDKTimer
instances don't do auto schedule. You have to manually call schedule
in order to start.
let timer = PDKTimer(timeInterval: 5.0, repeats: true){
// do something
}
timer.schedule()
PDKTimer
is implemented using GCD APIs. It isn't a Swift wrapper that uses NSTimer
on the inside. This allows not only executing the timer in a background queue, but dispatching the timed closure on a specific dispatch queue
.
Simply create your timer using :
let dispatchQueue = dispatch_queue_create("com.produkt.pdktimer.test", DISPATCH_QUEUE_SERIAL)
PDKTimer(timeInterval: 0.5, repeats: false, dispatchQueue: dispatchQueue){
// do something
}
and your closure will be executed on your custom queue
I really liked SwiftyTimer API from radex, that is a Swift wrapper for NSTimer
So I adopted the API that he suggests.
You can create repeating and non-repeating timers with PDKTimer.every
and PDKTimer.after
short-hand initializers
PDKTimer.every(5.seconds){
// executes every 5 seconds
}
PDKTimer.after(5.seconds){
// waits 5 seconds and then executes once
}
An extension of Double is also defined so you can define time intervals with Ruby-on-Rails-like helpers
500.milliseconds
1.second
2.5.seconds
5.seconds
10.minutes
1.hour
PDKTimer
short-hand initializers do auto schedule. So you don't need to manually call schedule
PDKTimer.every(5.seconds){
}
// no scheduling needed
Daniel García
https://github.com/fillito https://github.com/Produkt
Thanks to Javi Soto for writing MSWeakTimer. It helped me a lot understanding som GCD APIs to implement a timer
Thanks to Radek Pietruszewski for designing SwiftyTimer API, that I basically ripped off