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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to