NSTimer not precise enough - ios

I'm setting up timers to execute code once each one finished. However, it seems that the timing of NSTimer is not completely precise, after a while, the timers seem to finish a little too early.
I've set up the following to test the deviation.
(The start button is done in the Storyboard and linked to.)
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var startButton: UIButton!
//var mainTimer: NSTimer!
var counter: NSTimeInterval = 0.0
#IBAction func startButtonPressed(sender: AnyObject) {
startMainTimer()
startTimers()
}
func startMainTimer() {
let mainTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: #selector(ViewController.count), userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(mainTimer, forMode: NSRunLoopCommonModes)
}
func count() {
counter += 0.01
}
func startTimers() {
var lastTimeInterval: NSTimeInterval = 0.0
for _ in 0..<50 {
let timeInterval = lastTimeInterval + 1.0
lastTimeInterval = timeInterval
// Not setting up a repeating timer as intervals would be different in my original project where the deviation will be even worse.
let timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target: self, selector: #selector(ViewController.printTime), userInfo: nil, repeats: false)
NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
}
}
func printTime() {
print(counter)
}
}
After while, the timers will be early a tenth of a second or even more.
Should I be using NSDate for this to have better timing, or would that be overly complicated? How would the above look with NSDate?
Any help / pointers much appreciated!

It will never be perfect, but you can stop it from compounding by scheduling the next call at the end of the target selector. Calculate a new interval each time based on the current time and when you want it to trigger.
EDIT: I grabbed some code from a very old project to give the idea (that's why it's in Obj-C) -- You want something like this but not exactly:
- (void)constantIntervalTimer {
static const NSTimeInterval secondsBetweenMessageSend = 1.0;
if ( !self.timerShouldStop ) {
NSDate *startTime = [NSDate date];
// your code here, it should take less than a
// second to run or you need to increase the timer interval
// figure out the right time to preserve the interval
NSTimeInterval waitTime = secondsBetweenMessageSend -
[[NSDate date] timeIntervalSinceDate:startTime];
// in case your code runs in more than the interval,
// we just call back immediately
if (waitTime < 0.0)
waitTime = 0.0;
[NSTimer scheduledTimerWithTimeInterval: waitTime
target:self selector:#selector(constantIntervalTimer)
userInfo:nil repeats:NO];
}
}
Once you call this message, it will keep calling itself every second until you set a property called timerShouldStop to YES. You must declare and define this property and set it to NO in your init for this message to work.

Related

Timer using Swift only counts down for 20 seconds, not twenty minutes

I am creating a timer on my app in iOS which counts down from 20 minutes to 0 seconds, one second at a time. So far the timer works but only counts down for 20 seconds, not minutes. It also doesn't stop when it gets to zero. How can this be resolved?
import UIKit
class SkippingViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
#IBOutlet weak var startWorkoutButton: UIButton!
#IBOutlet weak var pauseWorkoutButton: UIButton!
var timer = Timer()
var counter = 20.00
var isRunning = false
override func viewDidLoad() {
super.viewDidLoad()
timeLabel.text = "\(counter)"
startWorkoutButton.isEnabled = true
pauseWorkoutButton.isEnabled = false
}
#IBAction func startWorkoutButtonDidTouch(_ sender: Any) {
if !isRunning {
timer = Timer.scheduledTimer(timeInterval: -0.01, target: self, selector: #selector(SkippingViewController.updateTimer), userInfo: nil, repeats: true)
startWorkoutButton.isEnabled = false
pauseWorkoutButton.isEnabled = true
isRunning = true
}
}
#IBAction func pauseWorkoutButtonDidTouch(_ sender: Any) {
startWorkoutButton.isEnabled = true
pauseWorkoutButton.isEnabled = false
timer.invalidate()
isRunning = false
}
#objc func updateTimer() {
counter -= 0.01
timeLabel.text = String(format: "%.01f", counter)
}
There's a couple of issues that I see with your code that prevent it from working the way you want.
First of all, if the timeInterval passed in the scheduledTimer method is negative, it will always lead to a timer being created that fires every 0.1 milliseconds (source: documentation).
What you want is your updateTimer to be called every second, so just pass 1.0 to the scheduledTimer method, e.g.:
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(SkippingViewController.updateTimer), userInfo: nil, repeats: true)
Furthermore, you want to invalidate this timer when 20 minutes have passed since starting the timer. So when setting up the timer you could keep track of the current time, and when that time + 20 minutes is more than the current time when called in updateTimer, you can invalidate the timer. With other words, instead of counting down from 20 minutes, we are counting from 0 til 20 minutes have passed!
Example code (didn't try compiling it, but should work, let me know if it doesn't):
import UIKit
class SkippingViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
#IBOutlet weak var startWorkoutButton: UIButton!
#IBOutlet weak var pauseWorkoutButton: UIButton!
var timer = Timer()
var countDownFromMinutes = 20
var timerStartTime: Date?
override func viewDidLoad() {
super.viewDidLoad()
timeLabel.text = "\(countDownFromMinutes):00"
startWorkoutButton.isEnabled = true
pauseWorkoutButton.isEnabled = false
}
#IBAction func startWorkoutButtonDidTouch(_ sender: Any) {
if !timer.isValid {
// run every second
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(SkippingViewController.updateTimer), userInfo: nil, repeats: true)
timerStartTime = nil
startWorkoutButton.isEnabled = false
pauseWorkoutButton.isEnabled = true
}
}
#IBAction func pauseWorkoutButtonDidTouch(_ sender: Any) {
startWorkoutButton.isEnabled = true
pauseWorkoutButton.isEnabled = false
timer.invalidate()
}
#objc func updateTimer() {
guard let startTime = timerStartTime else {
// first firing
timeLabel.text = "\(countDownFromMinutes):00"
timerStartTime = Date()
return
}
let now = Date()
let calendar = Calendar.current
// this ordinarily never returns nil, return gracefully if so
guard let endTime = calendar.date(byAdding: .minute, value: countDownFromMinutes, to: startTime) else {
return
}
let differenceMinuteSeconds = Calendar.current.dateComponents([.minute, .second], from: now, to: endTime)
// 20 minutes have passed since start
if now >= endTime {
timeLabel.text = "00:00"
timer.invalidate()
return
}
timeLabel.text = String(format: "%02d:%02d", differenceMinuteSeconds.minute ?? 0, differenceMinuteSeconds.seconds ?? 0)
}
}
Note that I also replaced the isRunning var you added with using timer.isValid as I believe it would achieve the same without introducing another variable.
Only thing that is probably left is regarding the text of label you are displaying. I am not sure what you actually want to display here. If you can add that in the comments I can suggest an approach for that.
You’ve set your timer’s interval to fire every 0.01 seconds, i.e. 100 times per second. And you’re adjusting the counter by 0.01 each time. So that means that it will decrement the counter at a rate of 1 per second. So a counter of 20 will expire in 20 seconds, not 20 minutes. If you want 20 minutes using your counter mechanism, you’d use 20 * 60.
A couple of other observations:
Timers are not guaranteed to fire at the requested interval. It’s safer to save the time to which you’re counting down (or the time you started) and calculate the time elapsed from that. Then you can update the label with a nice string representation of the time elapsed.
You’re firing your timer every hundredth of a second. But screens don’t generally update with that frequency. Besides, you’re just showing minutes and seconds, so updating more frequently than that offers no benefit.
If I were, though, showing milliseconds, and wanted an optimal timer frequency, I’d use a CADisplayLink rather than a timer. And, in the spirit of point 1, above, if you are using “start”/“stop” date to calculate how much time has elapsed, rather than a counter, you don’t have to worry about what this refresh rate is, since we’re no longer decrementing a counter.

Timer is not running more than 3 minutes in background

I am working on meditation app. In this app i have some musical content and some silent meditation section using timer. Timer is working fine when it is in foreground but it is running for only 3 min in background(when device is locked or user press home button to exit from the app). I am using swift4. What i have tried :
var timer: Timer!
var timeCounter : Int!
var backgroundTaskIdentifier: UIBackgroundTaskIdentifier?
var backgroundTask = BackgroundTask()
#IBOutlet weak var timeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: {
UIApplication.shared.endBackgroundTask(self.backgroundTaskIdentifier!)
})
backgroundTask.startBackgroundTask()
DispatchQueue.global(qos: .background).async {
let currentRunLoop = RunLoop.current
let timeInterval = 1.0
self.timer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(self.updateTimer), userInfo: nil, repeats: true)
self.timer.tolerance = timeInterval * 0.1
currentRunLoop.add(self.timer, forMode: .commonModes)
currentRunLoop.run()
}
}
}
#objc func updateTimer () {
timeCounter = timeCounter - 1
let minutes = Int(timeCounter) / 60 % 60
let seconds = Int(timeCounter) % 60
print("timeCounter", timeCounter)
if (timeCounter == 0){
if self.timer != nil {
self.timer?.invalidate()
self.timer = nil
player.play()
}
}
timeLabel.fadeTransition(0.4)
timeLabel.text = String(format: "%02i:%02i",minutes,seconds)
}
Thanks in Advance.
In general you get limited time from the OS to run in background. You can check and react to the background time left using:
UIApplication.shared.backgroundTimeRemaining
If conditions are good (device is unlocked, battery full ...) you typically get about 160-180 seconds.
You find detailed information in Apples documentation.
As you want to play audio, you can use "Plays Audio" background mode to not get cut by the OS:
Depending how you play audio, configuring the AudioSession might also improve things.
Edit:
How I understand now from your comment, you want your app to do something every 4 minutes. The only possiblility I see is to use the BackgroundFetch feature. This does not guarantee a fixed interval though.

Stopwatch not synced

Sorry if this is a newbie question, I am very new to iOS & Swift. I have a problem with the timer interval: I set 0.01 time interval but it doesn't correspond with the timer label, because 0.01 corresponds in one millisecond but it doesn't show it. So basically the timer is skewed.
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(updateStopwatch) , userInfo: nil, repeats: true)
#IBAction func startStopButton(_ sender: Any) {
buttonTapped()
}
func updateStopwatch() {
milliseconds += 1
if milliseconds == 100 {
seconds += 1
milliseconds = 0
}
if seconds == 60 {
minutes += 1
seconds = 0
}
let millisecondsString = milliseconds > 9 ?"\(milliseconds)" : "0\(milliseconds)"
let secondsString = seconds > 9 ?"\(seconds)" : "0\(seconds)"
let minutesString = minutes > 9 ?"\(minutes)" : "0\(minutes)"
stopWatchString = "\(minutesString):\(secondsString).\(millisecondsString)"
labelTimer.text = stopWatchString
}
func buttonTapped() {
if isTimerRunning {
isTimerRunning = !isTimerRunning
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(updateStopwatch) , userInfo: nil, repeats: true)
startStopButton.setTitle("Stop", for: .normal)
}else{
isTimerRunning = !isTimerRunning
timer.invalidate()
startStopButton.setTitle("Start", for: .normal)
}
}
Devices have maximum screen update rate (most are 60 fps), so there is no point in going faster than that. For maximum screen refresh rate, use a CADisplayLink rather than a Timer, which is coordinated perfectly for screen refreshes (not only in frequency, but also the timing within the screen refresh cycle).
Also don't try to keep track of the time elapsed by adding some value (because you are not guaranteed that it will be called with the desired frequency). Instead, before you start your timer/displaylink, save the start time and then when the timer/displaylink is called, display the elapsed time in the desired format.
For example:
var startTime: CFTimeInterval!
weak var displayLink: CADisplayLink?
func startDisplayLink() {
self.displayLink?.invalidate() // stop prior display link, if any
startTime = CACurrentMediaTime()
let displayLink = CADisplayLink(target: self, selector: #selector(handleDisplayLink(_:)))
displayLink.add(to: .current, forMode: .commonModes)
self.displayLink = displayLink
}
func handleDisplayLink(_ displayLink: CADisplayLink) {
let elapsed = CACurrentMediaTime() - startTime
let minutes = Int(elapsed / 60)
let seconds = elapsed - CFTimeInterval(minutes) * 60
let string = String(format: "%02d:%05.2f", minutes, seconds)
labelTimer.text = string
}
func stopDisplayLink() {
displayLink?.invalidate()
}
Note, CACurrentMediaTime() uses mach_time, like hotpaw2 correctly suggested, but does the conversion to seconds for you.
The time delay of a scheduledTimer is only approximate, and can differ from what is requested by many milliseconds, due to iOS overhead. A repeating Timer is even worse for timing, as any delay jitter errors will accumulate. So don't use a Timer for timing longer events.
A CADisplayLink is a more reliable timer, as it is synchronized to the 60 Hz display refresh (e.g. this is the maximum rate that any UILabel can be changed on devices other than the latest iPad Pros). There is no use trying to update a time display any faster (except possibly on the latest iPad Pros).
Also, do not use Date methods for timing, as they are not guaranteed to be monotonic when the device is connected to a network (as NTP can change the clock time right in the middle of your timing activity).
You should check any elapsed time measurement UI against one of the built-in timers such as mach_time. mach_absolute_time() is guaranteed to be monotonic, and not affected by NTP or other network activity.

getTimeElapsed in Swift with NSTimer

I'm a beginner in Swift and I'm facing a problem.
I want to create a class that contains a timer and a button
Everytime the button is tapped I have to restart the timer, if the time elapsed between the timer starts and the button is tapped is greater (or equal) to 400 milliseconds, I have to call the function in the selector.
But there isn't a "GetTimeElapsed()" method in swift and I don't know how to do it.
If you have some clue/tutorials it could be cool !
Thx guys
Few small steps:
Define the start time: (should happen at the same time you start the timer)
startTime = (NSDate.timeIntervalSinceReferenceDate())
Measure the time difference
let elapsed = NSDate.timeIntervalSinceReferenceDate() - startTime
By default, NSTimer does not have this feature.
Right way is do your job with dates and comparing it.
By the way, you can do a nice extension like this:
public extension NSTimer {
var elapsedTime: NSTimeInterval? {
if let startDate = self.userInfo as? NSDate {
return NSDate().timeIntervalSinceDate(startDate)
}
return nil
}
}
In this way, when you create your timer, you have to set userInfo:
let timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(foo), userInfo: NSDate(), repeats: true)
In your 'foo' method you can test elapsed time:
func foo() {
print(self.timer.elapsedTime)
}

WKInterfaceTimer used as a timer to countdown start and stop

I am trying to create a timer to countdown x minutes and y seconds.
I am computing the number of seconds and creating the InterfaceTimer like this:
timer.setDate(NSDate(timeIntervalSinceNow:Double(secondsValue+1)))
timer.stop()
after that I keep stoping it and starting it again and again, but the values are suddenly decreasing as "time(now) doesn't stop".
Eg: if the timer shows :55, I start it for 3sec and stop it, it shows :52, I wait 10seconds and then start it again, it starts from :42.
I can not save the value currently in the WKInterfaceTimer, so that I could start again from the same point. Everything I tried doesn't work. Did anyone work with the timer and it stayed at the same value after stopping it?
Yes the watchkit timer is a bit...awkward...and definitely not very intuitive. But that's just my opinion
You'll have to keep setting the date/timer each time the user chooses to resume the timer.
Remember, you'll also need an internal NSTimer to keep track of things since the current WatchKit timer is simply for display without having any real logic attached to it.
So maybe something like this...It's not elegant. But it works
#IBOutlet weak var WKTimer: WKInterfaceTimer! //watchkit timer that the user will see
var myTimer : NSTimer? //internal timer to keep track
var isPaused = false //flag to determine if it is paused or not
var elapsedTime : NSTimeInterval = 0.0 //time that has passed between pause/resume
var startTime = NSDate()
var duration : NSTimeInterval = 45.0 //arbitrary number. 45 seconds
override func willActivate(){
super.willActivate()
myTimer = NSTimer.scheduledTimerWithTimeInterval(duration, target: self, selector: Selector("timerDone"), userInfo: nil, repeats: false)
WKTimer.setDate(NSDate(timeIntervalSinceNow: duration ))
WKTimer.start()
}
#IBAction func pauseResumePressed() {
//timer is paused. so unpause it and resume countdown
if isPaused{
isPaused = false
myTimer = NSTimer.scheduledTimerWithTimeInterval(duration - elapsedTime, target: self, selector: Selector("timerDone"), userInfo: nil, repeats: false)
WKTimer.setDate(NSDate(timeIntervalSinceNow: duration - elapsedTime))
WKTimer.start()
startTime = NSDate()
pauseResumeButton.setTitle("Pause")
}
//pause the timer
else{
isPaused = true
//get how much time has passed before they paused it
let paused = NSDate()
elapsedTime += paused.timeIntervalSinceDate(startTime)
//stop watchkit timer on the screen
WKTimer.stop()
//stop the ticking of the internal timer
myTimer!.invalidate()
//do whatever UI changes you need to
pauseResumeButton.setTitle("Resume")
}
}
func timerDone(){
//timer done counting down
}

Resources