Re: NSTimer & Runloop questions
Re: NSTimer & Runloop questions
- Subject: Re: NSTimer & Runloop questions
- From: Joan Lluch-Zorrilla <email@hidden>
- Date: Fri, 20 Mar 2009 21:51:47 +0100
El 20/03/2009, a las 19:41, Robbie Hanson escribió:
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?
Just to let others to know, we have already solved this in the
CocoaAsyncSocket class pages of code.google.com.
Basically what we have to do is to use the userInfo property of the
timer to store a collection (for example a dictionary) with
information of its state. When we "invalidate" the timer by setting
its fire date to the distant future in the operationComplete method,
we should also set a flag in the userInfo dictionary so we will know
its state in the doTimeout method, in case the timer was already
scheduled to fire and it does. That's the easier approach.
Joan Lluch-Zorrilla
_______________________________________________
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