I set two timers in my application that repeat every few seconds. Everything works fine, except when it's time to cancel the timers. When the phone is locked, I want to cancel these timers and then recreate them when the phone is unlocked.
I use notifications to figure out when to invalidate / create timers.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notify_didBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notify_willResigneActive:) name:UIApplicationWillResignActiveNotification object:nil];
This notify_didBecomeActive method contains:
clockTicker = [[NSTimer scheduledTimerWithTimeInterval: 1
target: self
selector: @selector(showActivity)
userInfo: nil
repeats: YES] retain];
alarmTicker = [[NSTimer scheduledTimerWithTimeInterval: CONST_ALARMTIMER
target: self
selector: @selector(checkAndLaunchAlarm)
userInfo: nil
repeats: YES] retain];
This notify_willResigneActive method contains:
if (alarmTicker) {
[alarmTicker invalidate];
[alarmTicker release];
alarmTicker = NULL;
}
if (clockTicker) {
[clockTicker invalidate];
[clockTicker release];
clockTicker = NULL;
}
The problem is that when I debug this on the second timer, I get an error. The strange thing is that if I switch the orders of the timers (for example, clockTicker is invalidated first). I still have an error in the second timer.
What can i do wrong?
Thanks Leonardo