I get information from a server in my app with a secondsToEnd value and I start a counter after I've received this information.
My project contains a scrollview, so to get around locking my timer due to scrolling around I add the timer to the NSRunLoop in the following way:
[[NSRunLoop currentRunLoop] addTimer:timer
forMode:NSRunLoopCommonModes];
I made a NSTimer property called, how original, timer and this is the whole snippet of my startTimer function:
- (void)startTimer
{
if (_timer || [_timer isValid]) [_timer invalidate], _timer = nil, [_timer release];
NSTimer * timer = [[NSTimer alloc] initWithFireDate:[NSDate date]
interval:1.0
target:self
selector:@selector(timer:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer
forMode:NSRunLoopCommonModes];
[self setTimer:timer];
[timer release];
}
The reason for the check to invalidate in the start method is because after the secondsToEnd value hits 0, I receive a new one and I call startTimer again.
And in my dealloc method I have this:
if (_timer || [_timer isValid]) [_timer invalidate], _timer = nil, [_timer release];
But it doesn't get invalidated? What am I doing wrong?