Welcome to MacBoardz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

NSTimer problems

 
   Macintosh computer (Home) -> Programmer Help RSS
Next:  Power Macintosh G4 Tower  
Author Message
Lorenzo Thurman4

External


Since: May 23, 2004
Posts: 53



(Msg. 1) Posted: Wed Jan 16, 2008 4:04 pm
Post subject: NSTimer problems
Archived from groups: comp>sys>mac>programmer>help (more info?)

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 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

 >> Stay informed about: NSTimer problems 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
NSTimer/NSDate - Hey all-- I'm having problem creating an NSTimer using an NSDate to specify when it should fire. Below is an Obj-C message handler that I'm using to set it up. -(IBAction)setAlarm:(id)sender { NSString *dateString = [[[[alarmForm cellAtIndex:0]..

NSTimer in Java example? - Does anyone have a working example for using NSTimer in Java? My searches so far have yielded little, and none seems to actually work. -- -spiff Never be bullied into silence. Never allow yourself to be made a victim. Accept no one's definition of..

NSTimer and the modal window - This is turning out to be a tough one. . . I want this window to appear and display a text, then blank the text out after giving the user a brief look at it. Say, I show the window and then after a few seconds I send a message to the text field and....

Catching uncaught exceptions in NSTimer -

I'm having problems with sockets - I've been playing with sockets, and I have a problem. When I try to create a socket port like this: incomingSocket = [[NSSocketPort alloc] initWithTCPPort:PORT_NUMBER]; I find that 'incomingSocket' only gets created if PORT_NUMBER is zero (i.e..
   Macintosh computer (Home) -> Programmer Help All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]