NSTimer problem
NSTimer problem
- Subject: NSTimer problem
- From: email@hidden
- Date: Wed, 16 Jan 2008 16:22:19 -0600 (CST)
- Importance: Normal
I've found some issues with NSTimer and I could use a little help. I've
been doing some testing with it and I have some odd behavior. I initialize
a timer thusly:
-(IBAction)resetTimer:(id)sender{
if([self timer])
[[self timer] invalidate];
timer = [[NSTimer alloc] initWithFireDate:[NSDate
dateWithTimeIntervalSinceNow:currentInterval]
interval:currentInterval target:self
selector:@selector(timerCalled:)
userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[self setStringValue:[[timer fireDate] description]];
[timer fire];
[self setTimer:timer];
}
-(void)timerCalled:(id)sender{
if([self timer]){
NSLog(@"Fire date: %@", [[[self timer] fireDate] description]);
}
else
NSLog(@"Timer not valid!");
}
Store the timer in a instance variable and use accessors to manipulate it.
-(void)setTimer:(NSTimer*)tm{
[timer autorelease];
timer = [tm retain];
}
-(NSTimer*)timer{
return timer;
}
I have a window with a button that when clicked calls an IBAction that
invalidates the timer. When I click the button, the function is invoked(I
have an NSLog statement there), but the timer keeps running. And the
firedate matches the timestamp for the log entry. I would expect it to
display the next fire date.
Stepping through the debugger, I see that when inside my invalidate
function, the timer is null, 0x0.
-(IBAction)stopTimer:(id)sender{
NSLog(@"Calling invalidate");
if([timer isValid])
[timer invalidate];
[self resetInterval:nil];
}
I also have a matrix that when clicked, changes the repeat interval to the
tag value associated with one of its two buttons, (I set custom tag values
instead of the default 0, 1) and then restarts the timer.
-(IBAction)resetInterval:(id)sender{
if([[sender selectedCell] tag] != currentInterval){
currentInterval = [[sender selectedCell] tag];
[self resetTimer:nil];
count = 0;
}
}
When the matrix is clicked, instead of the first timer being invalidated,
I get a second timer firing on the new interval. The first timer is still
invalid, but firing anyway, and I apparently don't have a valid handle on
the second one as it can't be stopped either. I never have more than two
timers firing even after clicking the matrix a third or forth time.
I tried sublassing NSTimer, and overriding release, autorelease, and
dealloc, just to add log messages. Initializing my custom timer failed. I
eventually found a post that suggests subclassing NSTimer can't be done.
http://lists.apple.com/archives/Cocoa-dev/2006/Nov/msg00389.html
I'm not sure what's going on here. Can someone lend a hand? This is
running on 10.5.1 PPC. I'm going to do some testing this evening on 10.4
where I don't recall ever having any trouble with the NSTimer class
before, so I'm not sure if:
a) I screwed up (typical)
b) NSTimer is not quite right on Leopard (possible)
c) I should just use performSelector... (probably)
Also, I should note that I've been playing with the code to try to track
down the problem. What I have written above should be the original code,
but I'm re-writing it in Thunderbird, so there may be a syntactical error
here or there. I'm not at a Mac right now, so I can't test it, but it
should be the original code where I noticed the problem.
What I'm trying to do is create a timer that will send a given message to
the specified object at regular intervals. I also need to reset that timer
periodically as the interval changes.
Thanks
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden