Stopwatch run in background sleep mode - ios

I have built a basic app with a stopwatch on it. It runs off 3 buttons - play, pause, and reset on a navigation bar which write to a label called timerLabel.
The trouble is as soon as the app is closed or the iPhone goes into sleep mode the stopwatch stops. I've looked for the answer in other questions and it seems that the only way around it is to record the actual time when the app goes off and record the time when it reawakens and compare the difference - then write this to the label
Trouble is I have absolutely no idea how to incorporate that into my current code. I don't even know how to get the time. Here is my code from my view controller.
func updateTimer() {
time++
updateTimeDisplay()
}
func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
let h = seconds / 3600
let m = (seconds % 3600) / 60
let s = (seconds % 60)
return (h, m, s)
}
func pauseTimer() {
timer.invalidate()
}
func startTimer() {
if !timer.valid {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTimer"), userInfo: nil, repeats: true) }
else {
print("The Stopwatch has already started!")
}
}
func resetTimer() {
time = 0
timer.invalidate()
updateTimeDisplay()
}
func updateTimeDisplay() {
let (h, m, s) = secondsToHoursMinutesSeconds(time)
let timeToDisplay = String(format:"%02d:%02d:%02d", h, m, s)
timerLabel.text = timeToDisplay
}

You cannot use an NSTimer in the background, and your app will not run for long there anyway.
When the application enters background, you should stop the timer and take a note of the current time. Then when the application resumes again you should restart the timer, and calculate how long the app was in the background to update the display accordingly.

Related

Set a timer with variable intervals in swift

I need to set a specific timer asynchronously after executing an action like this:
calling my function (sending http request)
10 seconds after, sending another request
20 seconds after 2), sending another one
40 seconds after 3), another one
then send every 60 seconds another one
At any moment, I must be able to cancel my timer. Firstable I thought using DispatchQueue, but I see several post saying that it's not possible to cancel it.
Some post suggest to use DispatchWorkItem ( how to stop a dispatchQueue in swift ) but I'm not sur it fit my need (unless adding a sleep(10,20,40,60...) in each loop but will it not impact asynchronous part?).
Another answer from this post suggest to use Timer instead ( scheduledTimerWithTimeInterval ) with repeats:false, and invalidate it after each loop, but I didn't undertand how to do the loop in this case. Actually, here's my code, that just send a request after 10 seconds:
private func start() {
timer?.invalidate()
if(self.PCount > self.Intervals.count){
self.value = self.pollingIntervals.count-1
} else {
self.Value = self.Intervals[self.pCount]
}
print("set timer with \(pollingValue) as interval")
timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(pollingValue), repeats: false, block: { timer in
self.sessionManager.sendHit()
self.pollingCount+=1
})
}
The current goal is to do something like coroutine in Kotlin, like it work with this code :
private val Intervals = longArrayOf(10000,20000,40000,60000)
private var Count = 0
private fun start() {
currentJob = GlobalScope.launch {
while (true) {
delay(Intervals[if (Count > Intervals.size) Intervals.size - 1 else Count]) // 10,20,40 then every 60
session.sendHit()
pollingCount++
}
}
}
I'm not sure what solution is the most appropriate to my project
Here is a basic idea on how to approach the problem
struct RequestMananger {
var timers: [Timer] = []
mutating func startSequence() {
var delay = 10.0
sendRequest()
timers.append(scheduleTimer(delay))
delay += 20
timers.append(scheduleTimer(delay))
delay += 40
timers.append(scheduleTimer(delay))
delay += 60
timers.append(scheduleTimer(delay, repeats: true))
}
private func scheduleTimer(_ delay: TimeInterval, repeats: Bool = false) -> Timer {
return Timer.scheduledTimer(withTimeInterval: delay, repeats: false, block: { timer in
self.sendRequest()
})
}
func sendRequest() {
}
func cancelTimers() {
timers.forEach { $0.invalidate() }
}
}

How to keep two timers in sync

I'm still pretty new to coding and Swift. So bear with me.
Problem Statement : I've got a stopwatch style app that has two concurrent timers start at the same time and display in a mm:ss.SS format, but one is designed to reset to 0 at specific intervals automatically while the other keeps going and tracks total time.
Similar to a "lap" function but it does it automatically. The problem I've encountered is that occasionally the timers aren't perfectly synced up when the user pauses the timers. Since the reset happens at an exact second, both timers should have identical hundredths of a second, while the seconds and minutes will obviously be different. But sometimes the hundredths will be off by .01 or more.
Now, I know Timer isn't designed to be perfectly accurate, and in practice on my app this isn't even a huge deal. My timer doesn't even need to be accurate to the hundredth of a second, and while running it's not noticeably off at all, only while paused. I could display fewer decimal places or none at all, but I prefer the style of showing the hundredths since it fits in well with the stock timer app style.
So if there's a way to make this work, I'd like to keep it.
Screenshot : screenshot
What I tried :
#IBAction func playPauseTapped(_ sender: Any) {
if timerState == .new {
//start new timer
startCurrentTimer()
startTotalTimer()
currentStartTime = Date.timeIntervalSinceReferenceDate
totalStartTime = Date.timeIntervalSinceReferenceDate
timerState = .running
//some ui updates
} else if timerState == .running {
//pause timer
totalTimer.invalidate()
currentTimer.invalidate()
timerState = .paused
pausedTime = Date()
//other ui updates
} else if timerState == .paused {
//resume paused timer
let pausedInterval = Date().timeIntervalSince(pausedTime!)
pausedIntervals.append(pausedInterval)
pausedIntervalsCurrent.append(pausedInterval)
pausedTime = nil
startCurrentTimer()
startTotalTimer()
timerState = .running
//other ui updates
}
}
func startTotalTimer() {
totalTimer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(runTotalTimer), userInfo: nil, repeats: true)
}
func startCurrentTimer() {
currentTimer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(runCurrentTimer), userInfo: nil, repeats: true)
}
func resetCurrentTimer() {
currentTimer.invalidate()
currentStartTime = Date.timeIntervalSinceReferenceDate
pausedIntervalsCurrent.removeAll()
startCurrentTimer()
}
#objc func runCurrentTimer() {
let currentTime = Date.timeIntervalSinceReferenceDate
//calculate total paused time
var pausedSeconds = pausedIntervalsCurrent.reduce(0) { $0 + $1 }
if let pausedTime = pausedTime {
pausedSeconds += Date().timeIntervalSince(pausedTime)
}
let currentElapsedTime: TimeInterval = currentTime - currentStartTime - pausedSeconds
currentStepTimeLabel.text = format(time: currentElapsedTime)
if currentElapsedTime >= recipeInterval {
if recipeIndex < recipeTime.count - 1 {
recipeIndex += 1
//ui updates
//reset timer to 0
resetCurrentTimer()
} else {
//last step
currentTimer.invalidate()
}
}
}
#objc func runTotalTimer() {
let currentTime = Date.timeIntervalSinceReferenceDate
//calculate total paused time
var pausedSeconds = pausedIntervals.reduce(0) { $0 + $1 }
if let pausedTime = pausedTime {
pausedSeconds += Date().timeIntervalSince(pausedTime)
}
let totalElapsedTime: TimeInterval = currentTime - totalStartTime - pausedSeconds
totalTimeLabel.text = format(time: totalElapsedTime)
if totalElapsedTime >= recipeTotalTime {
totalTimer.invalidate()
currentTimer.invalidate()
//ui updates
}
}
func format(time: TimeInterval) -> String {
//formats TimeInterval into mm:ss.SS
let formater = DateFormatter()
formater.dateFormat = "mm:ss.SS"
let date = Date(timeIntervalSinceReferenceDate: time)
return formater.string(from: date)
}
You should use a single timer. And when you need a reset to zero, save the current time to a variable.
When presenting the time in the UI, calculate the difference between the running total timer, and the time you saved previously.

Labels displaying countdown sometimes out of sync after pausing. Rounding errors?

I have an app that does a countdown with a Timer. The countdown tracks multiple steps (all at the same intervals) as well as the total time left, and updates 2 separate UILabels accordingly. Occasionally, the labels will be out of sync.
I can't say for sure, but I think it might be only happening when I pause the countdown sometimes, and usually on steps later than the first step. It's most apparent on the last step when the two labels should be displaying the same exact thing, but will sometimes be 1 second off.
The other tricky thing is that sometimes pausing and resuming after the time has gone out of sync will get it back in sync.
My guess is I'm getting something weird happening in the pause code and/or the moving between steps, or maybe the calculating and formatting of TimeIntervals. Also I'm using rounded() on the calculated TimeIntervals because I noticed only updating the timer every 1s the labels would freeze and skip seconds a lot. But I'm unsure if that's the best way to solve this problem.
Here's the relevant code. (still need to work on refactoring but hopefully it's easy to follow, I'm still a beginner)
#IBAction func playPauseTapped(_ sender: Any) {
if timerState == .running {
//pause timer
pauseAnimation()
timer.invalidate()
timerState = .paused
pausedTime = Date()
playPauseButton.setImage(UIImage(systemName: "play.circle"), for: .normal)
} else if timerState == .paused {
//resume paused timer
guard let pause = pausedTime else { return }
let pausedInterval = Date().timeIntervalSince(pause)
startTime = startTime?.addingTimeInterval(pausedInterval)
endTime = endTime?.addingTimeInterval(pausedInterval)
currentStepEndTime = currentStepEndTime?.addingTimeInterval(pausedInterval)
pausedTime = nil
startTimer()
resumeAnimation()
timerState = .running
playPauseButton.setImage(UIImage(systemName: "pause.circle"), for: .normal)
} else {
//first run of brand new timer
startTimer()
startProgressBar()
startTime = Date()
if let totalTime = totalTime {
endTime = startTime?.addingTimeInterval(totalTime)
}
currentStepEndTime = Date().addingTimeInterval(recipeInterval)
timerState = .running
playPauseButton.setImage(UIImage(systemName: "pause.circle"), for: .normal)
currentWater += recipeWater[recipeIndex]
currentWeightLabel.text = "\(currentWater)g"
}
}
func startTimer() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(runTimer), userInfo: nil, repeats: true)
}
#objc func runTimer() {
let currentTime = Date()
guard let totalTimeLeft = endTime?.timeIntervalSince(currentTime).rounded() else { return }
guard let currentInterval = currentStepEndTime?.timeIntervalSince(currentTime).rounded() else { return }
//end of current step
if currentInterval <= 0 {
//check if end of recipe
if recipeIndex < recipeWater.count - 1 {
//move to next step
totalTimeLabel.text = totalTimeLeft.stringFromTimeInterval()
currentStepEndTime = Date().addingTimeInterval(recipeInterval)
startProgressBar()
currentStepTimeLabel.text = recipeInterval.stringFromTimeInterval()
stepsTime += recipeInterval
recipeIndex += 1
//update some ui
} else {
//last step
currentStepTimeLabel.text = "00:00"
totalTimeLabel.text = "00:00"
timer.invalidate()
//alert controller saying finished
}
} else {
//update time labels
currentStepTimeLabel.text = currentInterval.stringFromTimeInterval()
totalTimeLabel.text = totalTimeLeft.stringFromTimeInterval()
}
}
extension TimeInterval {
func stringFromTimeInterval() -> String {
let time = NSInteger(self)
let seconds = time % 60
let minutes = (time / 60) % 60
return String(format: "%0.2d:%0.2d",minutes,seconds)
}
}
EDIT UPDATE: I tried a few different things but still kept having the same issue. I started testing with printing the TimeInterval and the formatted string to compare and see what's off. It's definitely some sort of rounding error.
Total - 173.50678288936615 / 02:54
Step - 39.00026595592499 / 00:39
Total - 172.5073879957199 / 02:53
Step - 38.00087106227875 / 00:38
Total - 171.1903439760208 / 02:51
Step - 36.68382704257965 / 00:37
Total - 170.19031596183777 / 02:50
Step - 35.683799028396606 / 00:36
As you can see, the total time skips from 2:53 to 2:51, but the step timer remains consistent. The reason is the TimeInterval for total goes from 172.5 which gets rounded up, to 171.19 which gets rounded down.
I've also watched the timer count down without touching pause, and it remains in sync reliably. So I've narrowed it down to my pause code.
Fixed my issue and posting here for posterity. I ended up making my totalTimeLeft and currentInterval global properties. Then, on pause and resume, instead of tracking the paused time and adding it to endTime, I just used the totalTimeLeft and currentInterval values that are still stored from the last Timer firing and doing endTime = Date().addingTimeInterval(totalTimeLeft) and the same with the interval time. This got rid of the paused time adding weird amounts that would mess up the rounding.

Updating UILabel when timer counts down each second in Swift 4.2

New guy here teaching myself Swift. Building my first personal app and have hit a wall after several searches on here, youtube, and google. This is my first time posting a question (as I've been able to find my other answers on here).
I'm having issues with my timer updating on the UILabel. I've managed to find code on older versions of swift that get the timer to run and count down. I then figured out how to break the seconds down to minutes and seconds.
But what I find is when I run the app, the timer shows "30:0" (a different issue I need to figure out) and never counts down. When I leave the page in the simulator and come back, it's only then that the UILabel updates.
I know viewdidload only loads upon the first moment the page opens. I'm having a hard time figuring out how to get the UILabel to update every time a second changes. I'm not sure what code to implement.
Thank you so much!
import UIKit
var timer = Timer()
var timerDuration: Int = 1800
// This converts my timeDuration from seconds to minutes and seconds.
func secondsToHoursMinutesSeconds (seconds : Int) -> (h: Int, m : Int, s : Int) {
return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}
class lyricWriteViewController: UIViewController {
//This takes the function to convert minutes and seconds and accepts an input, which I've chosen the variable timeDuration (which is currently 1800 seconds.
var theTimer = (h: 0, m: 0, s: 0)
#IBOutlet weak var countdownTimer: UILabel!
#IBOutlet weak var randomLyric: UILabel!
#IBOutlet weak var titleInput: UITextField!
#IBOutlet weak var lyricInput: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
//This line takes a random array number and shows it on the textlabel.
randomLyric.text = oneLiner
theTimer = secondsToHoursMinutesSeconds(seconds: timerDuration)
//This is the code the does the counting down
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(lyricWriteViewController.counter), userInfo: nil, repeats: true)
}
#objc func counter() {
timerDuration -= 1
// Below is the timer view I've created. It has converted seconds to minutes and seconds but the screen won't refresh. Also, when the seconds number hits zero, it does "0" instead of "00".
let displayTimer = "\(theTimer.m) : \(theTimer.s)"
countdownTimer.text = String(displayTimer)
//When the timer hits 0, it stops working so that it doesn't go into negative numbers
if timerDuration == 0 {
timer.invalidate()
}
}
func submitlyricsButton(_ sender: UIButton) {
//I will eventually tie this to a completed lyric tableview.
}
}
var timer: Timer?
var totalTime = 120
private func startOtpTimer() {
self.totalTime = 120
self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
#objc func updateTimer() {
print(self.totalTime)
self.lblTimer.text = self.timeFormatted(self.totalTime) // will show timer
if totalTime != 0 {
totalTime -= 1 // decrease counter timer
}
else {
if let timer = self.timer {
timer.invalidate()
self.timer = nil
}
}
}
func timeFormatted(_ totalSeconds: Int) -> String {
let seconds: Int = totalSeconds % 60
let minutes: Int = (totalSeconds / 60) % 60
return String(format: "%02d:%02d", minutes, seconds)
}
It is because you are not updating the theTimer value. As viewDidLoad() is called once it is not working fine, you need to update theTimer value after deducting 1 from it.
So move this line :
theTimer = secondsToHoursMinutesSeconds(seconds: timerDuration)
in counter() funtion after timerDuration -= 1. So your function should look like this :
#objc func counter() {
timerDuration -= 1
if timerDuration == 0 {
timer.invalidate()
} else {
theTimer = secondsToHoursMinutesSeconds(seconds: timerDuration)
let displayTimer = "\(theTimer.m) : \(theTimer.s)"
countdownTimer.text = String(displayTimer)
}
}
Also move all of this inside controller:
var timer = Timer()
var timerDuration: Int = 1800
// This converts my timeDuration from seconds to minutes and seconds.
func secondsToHoursMinutesSeconds (seconds : Int) -> (h: Int, m : Int, s : Int){
return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)}
As timerDuration is global you will have to kill the app and run it again to see the timer working again.
Replace countdownTimer.text = String(displayTimer) with
DispatchQueue.main.async {
countdownTimer.text = String(displayTimer)
}
What I think is happening here is since countdownTimer.text = String(displayTimer) is not running on the main thread it is not updating immediately. It does however after a period of time (Like you said, when when you traverse the screen).

Timer in background is very slow

I'm working with timer and I've try to make it works in background. On simulator this works fine but on my device (iOS 11) it's very slow: 1 seconde became 5 or 6 secondes...
This is my code for run application in background:
backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(
expirationHandler:
{UIApplication.shared.endBackgroundTask(self.backgroundTaskIdentifier!)}
)
Info.plist:
Application does not run in background : NO
How can I make it works?
EDIT:
This is my timer code:
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self,
selector:#selector(ViewController.updateTimer), userInfo: nil, repeats: true)
EDIT 2:
func updateTimer () {
var j = 0
for _ in rows {
if (rows[j]["Playing"] as! Bool == true ) {
rows[j]["time"] = (rows[j]["time"] as! Double + 0.01) as AnyObject
// print(rows[j]["time"]) - PRINT OUTPUT HERE
rows[j]["lastTime"] = (rows[j]["lastTime"] as! Double + 0.01) as AnyObject
}
if (rows[j]["lastTime"] as! Double > 60.0) {
min[j] += 1
rows[j]["lastTime"] = 0.00 as AnyObject
}
j += 1
}
}
Instead of just printing your output, which might be executed from a different thread, print it from the main thread. That's why you might see the delay:
DispatchQueue.main.async {
print(rows[j]["time"])
}
Your timer interval is too short. Timers have a resolution of 50-100 ms, so your interval (10 ms) can't get executed so fast. I think when your app is in the background this effect increases.
Take a look at this answer: https://stackoverflow.com/a/30983444/5613280

Resources