How can I know if a fired timer has already been "invalidated" using
setFireDate? Consider the following pseudo-code:
- (void)dequeueNextOperation
{
// IF there is another operation in the queue
// Dequeue and start opertation...
// Start timeout timer for operation
// Instead of creating a new timer for every operation, we'd like to
simply recycle the same one
[timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:op->timeout]];
}
// Called when the operation has completed
- (void)operationComplete
{
// "invalidate" timer
// We have learned that this will not always prevent the doTimeout:
method from being called
[timer setFireDate:[NSDate distantFuture]];
// Notify delegate of completed op
[self performSelector:@selector(dequeueNextOperation) afterDelay:0.0];
}
- (void)doTimeout:(NSTimer *)aTimer
{
// Is there anyway to tell if this timer has already
// been "invalidated" in the operationComplete method?
}
Perhaps there is something clever I can do with the timer's userInfo
variable or something?
On Mar 19, 2009, at 11:01 PM, Ken Thomases wrote:
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