Re: NSTimer & Runloop questions
Re: NSTimer & Runloop questions
- Subject: Re: NSTimer & Runloop questions
- From: Ken Thomases <email@hidden>
- Date: Thu, 19 Mar 2009 23:01:22 -0500
On Mar 19, 2009, at 1:51 PM, Robbie Hanson wrote:
However I'm not sure how safe it is to use the setFireDate method of
NSTimer. Consider the following code:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(timeout:)
userInfo:nil
repeats:NO];
[NSThread sleepForTimeInterval:2.0];
// This line will properly prevent the timer from firing
//[timer invalidate];
// This line will NOT
[timer setFireDate:[NSDate distantFuture]];
Why is this exactly?
At a guess, because the timer has already fired. You are changing the
time when it will _next_ fire. But the timer is already marked as
having fired. When the run loop next gets a chance, it will invoke
the callback for the previous firing. Invalidating the timer actually
removes it from the run loop, preventing that.
Regards,
Ken
_______________________________________________
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