TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Running a timer in the background

Forums > SwiftUI

I'm beginning to wonder if I have identified something that may not be possible.

With the help of Paul's videos and this site, I have been playing around with Swift and learning a new language. I'm hooked! I'm creating an app for my son to track some routines to help with his OCD. He has to complete activities within a time limit to get points which equate to reward.

I have successfully generated a stopwatch timer in one of my views. However, when I suspend the app, or lock my iOS screen, the timer pauses.

I've noticed that I can add the Background capability to my project, but not sure how to use any of its functions.

Is it possible to create code that permits the user to start the timer, and it continues if the app goes to background or phone sleeps? If so, any clues or tips would be appreciated.

Paul

3      

You might want to look at Counting down with a Timer to see if that helps

3      

hi Paul,

i'll offer some code i put together in a project just yesterday (that i co-opted from a previous project). please look at my ShoppingList project where you can find what could be a stand-alone component for any app for a timer (in my case, it's a separate tab in the app that just runs run a timer). i've defined a boolean in that code that offers you a choice: count time while inactive or not.

basic idea: keep track of the time when you start the timer. when the app goes into the background, just record the current time, figure out how much time you've been running since you last started, and kill the Timer. the timer is now in a suspended state.

when you become active again, restart the timer (instantiating a new Timer) and reset the start time to either the current time (if you want time not to count when you were inactive) or the time you stopped previously (if you were suspended and you're counting time while inactive).

not sure it's exactly what you were looking for, though.

hope this code helps,

DMG

5      

Thanks DMG, I was thinking last night about recording the system time in to a variable and workinv ouf ghe difference between now and the recorded time, but your dolution sounds elegant! Thanks

3      

Thanks again DMG. I saw your code and adopted it in my app and works flawlessly.

5      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.