Reputation: 1452
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?!?
Upvotes: 0
Views: 48