I'm a total noobie coming to OSX cocoa programming from php, and am having a lot of trouble with NSTimer. I'm trying to create an application that keeps a user-specified duration timer running in the background and then does something when that timer expires (like a countdown timer).
When its in the foreground my application works as it should. But if it goes into the background (ie: I click on another application window), the timer becomes entirely erroneous, and the countdown seems to progress on an ad hoc basis. Then, once I re-focus on this application, the timer seems to re-start. But if the pre-specified amount of time has elapsed while the application is in background, the application doesn't know it.
Here is the basic code I'm calling from within the main run loop:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateStatus) userInfo:nil repeats:YES];
-(void) updateStatus {
if([self.totalSeconds]>0) {
self.totalSeconds=self.totalSeconds-1;
} else {
//perform end of timer methods
}
}
What am I doing wrong?
Thanks in advance for your time / assistance.
-M