0

I have an macOS app that have timer and increase number every second. Every 60 second I show a notification with some message.

I found that my macOS Swift app runs with some delay, and after ~10 minutes started to sleep (no new a;lets appear anymore).

I have found some solutions, like this:

private func startTimer() {
    timer = Timer.scheduledTimer(
        timeInterval: 1.0,
        target: self,
        selector: #selector(timerTick),
        userInfo: nil,
        repeats: true
    )
    RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)
}

But this command don't helped me to fix the lag with background run of timer: RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)

Need some experienced Swift guy who can help me to solve this interesting challenge. I need to count seconds in background app correctly, because my app shows alert messages every few minutes, and if it can't show me these alerts correctly - my app will work not as I expected.

Thank you!

Anton Danilchenko
  • 2,318
  • 1
  • 25
  • 24
  • Sound like you might be running into Apple's App Nap feature ( http://arstechnica.com/apple/2013/10/os-x-10-9/13/ ). Instead of scheduling a once-per-second repeating timer and incrementing a counter, you might be better off scheduling a single-shot timer that will call your callback just once, after a few minutes, at which point you can schedule the next timer callback. – Jeremy Friesner Oct 12 '16 at 15:31
  • Related: http://stackoverflow.com/questions/27653939/disable-app-nap-in-swift. – Martin R Oct 12 '16 at 15:32
  • Still didn't found solution for the issue. – Anton Danilchenko Oct 28 '16 at 09:35

1 Answers1

0

My bet is that you're running into App Nap. You do have some control about telling the system your process should not be wound down with App Nap via NSProcessInfo.

You may also be able to try to disable App Nap defaults write <app domain name> NSAppSleepDisabled -bool YES... but not sure if that still works or not (App Nap was introduced in OS X 10.9, so the three years time may have disabled the disable function...)

RyanWilcox
  • 13,890
  • 1
  • 36
  • 60