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 14:18:51 -0800
This seems like a good way to go. Thanks for the tip.
On Nov 8, 2007, at 12:01 PM, Michael Nickerson wrote:
On Nov 8, 2007, at 1: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];
John, instead of using a timer you could just use a delayed
perform. You can cancel said perform when you get a new call before
re-calling it.
The code would be something like this:
- (void)cleanupMethod:(id)sender
{
//Your clean-up code here
}
- (void)doCleanup
{
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(cleanupMethod:) object:self];
[self performSelector:@selector(cleanupMethod:) withObject:self
afterDelay:0.25];
}
Of course, there's caveat's doing it this way: your cleanup method
will need to know what objects are actually being cleaned up, as
it's not passed that info. Doing it this way, though, your cleanup
method would only be called once, and would only get called when
objects actually need to be cleaned up.
I'm not entirely certain how you have your code setup, so I don't
know if this would be the best method to use. But if you have some
sort of central object that does cleanup of other objects, this
would be a good alternative.
I use this with sliders that are updating something in the GUI
mostly, so that it's not updating every single time the slider's
value changes. You can, of course, adjust the delay to whatever
works best for you, and if you want it to run in run loop modes
other than NSDefaultRunLoopMode you can add in the inModes: option
to the performSelector:... method and pass it an array of run loop
modes you'd like it to run in.
--------------------------------------
Darkshadow
(aka Michael Nickerson)
http://www.nightproductions.net
_______________________________________________
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