Re: Best pattern to follow for scheduling an event
Re: Best pattern to follow for scheduling an event
- Subject: Re: Best pattern to follow for scheduling an event
- From: John Stiles <email@hidden>
- Date: Thu, 8 Nov 2007 10:27:48 -0800
On Nov 8, 2007, at 10:25 AM, glenn andreas wrote:
On Nov 8, 2007, at 12:05 PM, John Stiles wrote:
I have a method that needs to schedule a "cleanup pass" to occur
in the near future. The method might be called once, ten times, or
a hundred times in a row, but I only need to clean up one time. To
implement this, I used the following pattern, and I'm wondering if
it was the best way to go.
First, when the object is first created, I create a timer. I
scheduled its fire date to be in the distant, distant future:
m_deferredFixupTimer = [[NSTimer
scheduledTimerWithTimeInterval:DBL_MAX
target:myObject
selector:@selector(doFixUp:)
userInfo:NULL
repeats:YES] retain];
Then, when my method is called, I schedule the timer to fire
immediately:
[m_deferredFixupTimer setFireDate:[NSDate date]];
What about:
m_deferredFixupTimer = [[NSTimer alloc] initWithFireDate: [NSDate
distantFuture] interval: 0 target: myObject selector: @selector
(doFixUp:) userInfo: NULL repeats: NO];
and then to get it to fire immediately
[m_deferredFixupTimer fire];
This defers the fire to the next run-through of the event loop,
which for my purposes is sufficient. It only fires one time.
Or if you want it to be the next event loop
[[NSRunLoop currentRunLoop] addTimer: m_deferredFixupTimer
forMode: NSDefaultRunLoopMode];
[m_deferredFixupTimer setFireDate: [NSDate date]];
Would this work more than once?
The current code works if you call it multiple times in a row.
With a non-repeating timer, the docs say it will invalidate itself as
soon as it has fired.
_______________________________________________
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