Timer only working in background temporarily.

Hello all,

I am creating a run tracker and want to enable background capability for the timer and location.

The timer temporarily works in the background but then times out between 30 and 60 seconds and doesn't continue counting until it's reopened.

I have tried saving the time its closed and reopened and calculating the difference between them but this doesn't seem to work.

My code is attached and any help would be appreciated soo much as I've been stuck with this for a few weeks.

I've put the whole view controller in so some of the code isn't relevant but thought it may be helpful to have it all.

Thank you in advance!



The event dispatching in iOS changes its behavior in background and Timer does not work in background.
What you have observed as temporarily works is just an accident.

You may need to re-design your app according to the fact.

I have tried saving the time its closed and reopened and calculating the difference between them but this doesn't seem to work.

One possible strategy. Why don't you go on with it?


By the way, your code is hard to read. I recommend you to follow the coding rule of Swift when your write code in Swift.
Thanks for the reply! Sadly saving the times doesn’t seem to be working for it.

thank you for the advice, I’ll certainly be taking it up.
Timer only working in background temporarily.
 
 
Q