Skip navigation.
 
mlRe: Best pattern to follow for scheduling an event
FROM : John Stiles
DATE : Thu Nov 08 23:18:51 2007

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
>
>

Related mailsAuthorDate
mlBest pattern to follow for scheduling an event John Stiles Nov 8, 19:05
mlRe: Best pattern to follow for scheduling an event j o a r Nov 8, 19:18
mlRe: Best pattern to follow for scheduling an event Jean-Daniel Dupas Nov 8, 19:22
mlRe: Best pattern to follow for scheduling an event glenn andreas Nov 8, 19:25
mlRe: Best pattern to follow for scheduling an event John Stiles Nov 8, 19:27
mlRe: Best pattern to follow for scheduling an event glenn andreas Nov 8, 19:37
mlRe: Best pattern to follow for scheduling an event John Stiles Nov 8, 19:43
mlRe: Best pattern to follow for scheduling an event David Spooner Nov 8, 19:47
mlRe: Best pattern to follow for scheduling an event glenn andreas Nov 8, 19:57
mlRe: Best pattern to follow for scheduling an event David Spooner Nov 8, 19:57
mlRe: Best pattern to follow for scheduling an event John Stiles Nov 8, 19:58
mlRe: Best pattern to follow for scheduling an event John Stiles Nov 8, 20:03
mlRe: Best pattern to follow for scheduling an event Ken Ferry Nov 8, 20:49
mlRe: Best pattern to follow for scheduling an event Army Research Lab Nov 8, 20:59
mlRe: Best pattern to follow for scheduling an event Michael Nickerson Nov 8, 21:01
mlRe: Best pattern to follow for scheduling an event John Stiles Nov 8, 23:18