2

I'm making a Clock app in swift for OS X. I have a Timer in my Clock app that works kinda like the one in iOS. My problem is whenever I put my app into the background or minimize it the application kinda freezes or stops counting after being in the background for a minute or so. It starts working right where it left off when I bring it into the foreground. Is there anyway I can make my application run in the background? I've looked and haven't found anything that works.

Thanks to anyone who can help.

Nathanael Carper
  • 302
  • 3
  • 17

1 Answers1

1

Are you relying on the timer to "count" the seconds? You should only use the timer to provoke an update of the display. When the timer fires, you should check the actual time and calculate what should be displayed based on that time, not how many times your timer has fired. It should never be the case that your app "starts working right where it left off" (if, by that, you mean that it shows the same time as when it was backgrounded and starts counting up from there again).

What's probably happening is that your app is being put into App Nap. Also see the Energy Efficiency Guide for Mac Apps.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • 2
    I want to know how to keep my app from being put into App Nap. – Nathanael Carper Feb 25 '16 at 15:24
  • Why? If it's just a clock, why is it important that it keep doing work when it's not visible? When it is visible, the system will keep giving it time to let it update, but less frequently. Should still be frequent enough to update. But you have to do your updates the right way, as I described. – Ken Thomases Feb 25 '16 at 16:21
  • Uh, maybe I didn't clearly state my question, if so I apologize, I'm not very good at wording sentences. But I did say I have a Timer like the one in iOS. I want the Timer to continue to update in the background. That is important, if it doesn't update in the background then my app becomes unreliable and will get a bad rating. Although my application is called "Clock", my app has a Timer, Stop Watch, and Alarm Clock, all of which need to update in the background in order to continue to stay accurate. My app is kinda like the Clock app in iOS. Hopefully this helps. – Nathanael Carper Feb 26 '16 at 01:04