0

I've searched endlessly for this answer and am surprised I haven't found anything (yet).

What I have is an app with a super simple timer function; start the clock timer, and seconds start to click up. If the app is in the foreground, the timer fires every second and then updates the UI accordingly.

If I minimize it or it goes into the background, then after about 30 seconds or so... the selector action only fires every 10 to 20 seconds... I haven't sat and tested how long the intervals get, but it clearly is more than 1 second.

I've simplified the code below to scrape out all the unnecessary U.I. .stringValue calls better readability. So here's what I've got:

@property (assign) int seconds;

@interface ViewController : NSViewController {
    NSTimer *myTimer;
    bool timerRunning;
}

-(void)btnAction { 
    myTimer = [NSTimer
                  scheduledTimerWithTimeInterval: 1.0
                  target: self
                  selector: @selector(timerAction)
                  userInfo: nil
                  repeats: YES];
}

-(void)timerAction {
    _seconds++;
    NSLog(@"seconds: %d", _seconds);
}

I've also toyed with GCD a bit with this:

dispatch_async(dispatch_get_main_queue(), ^{
     myTimer = [NSTimer
                  scheduledTimerWithTimeInterval: 1.0
                  target: self
                  selector: @selector(timerAction)
                  userInfo: nil
                  repeats: YES];
});

But ended up with the same results.

Lastly... I have a similar (albeit different) app on iOS that had similar issues that was solved after I added this to the AppDelegate:

[[UIApplication sharedApplication]
setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

I've looked for something similar in the macOS world but to no avail.

So... hit me -- what the heck am I doing wrong here?!?

Drew
  • 1,422
  • 1
  • 18
  • 29
  • 1
    You should never determine elapsed time by counting timer firings. Use the timer to provoke UI updates. In the UI update, compute the time to display (perhaps by taking the difference between the current time and a start time) and display it. Then, it's fine and desirable for the timer to slow or stop when the UI is invisible. – Ken Thomases Sep 10 '18 at 19:24
  • Thanks for the response @KenThomases. I can see where using the timestamps may be beneficial from a data standpoint, but perhaps I'm not fully grasping the concept you're describing. I need the UI to update every second, so if the timer itself is stalling and not firing every second... that's my real issue (I think). I've noticed that this happens both when it's up and open off to the side (but not the active app) and also when it's minimized to the dock. After bringing to the forefront -- I can see the nslog's firing each second again. – Drew Sep 13 '18 at 17:47
  • Good Lord... I completely missed the link talking about disabling App Napp up above. Thanks again Ken. Sheesh... https://stackoverflow.com/questions/19847293/disable-app-nap-in-macos-10-9-mavericks-application – Drew Sep 13 '18 at 18:07

0 Answers0