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:43:06 -0800
I think I'm not explaining myself well enough.
The typical pattern is that my method will get spammed a hundred
times in quick succession. After the burst of calls has completed, I
want to do fix-up. Technically, I could do my fix-ups after every
single call, but that would probably be slow so I want to avoid it.
This doesn't mean that the method will never get called again—much
later on in app execution, it's quite possible that my method will
suddenly get spammed again with hundreds of calls, and then I will
want to do fix-ups again.
The goal here is to avoid performing fix-up after every single method
call because I know they come in bursts, but there's no easy way to
know when the burst ends. It's pretty likely that if the event loop
is running, though, the burst of changes has run its course.
Consider something like text layout. An NSTextView doesn't reflow its
entire contents every time you make a trivial change—it defers the
big reflowing until "sometime later," again usually when the event
loop gets a chance to run.
On Nov 8, 2007, at 10:37 AM, glenn andreas wrote:
On Nov 8, 2007, at 12:27 PM, John Stiles wrote:
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.
So, assuming I'm following this correctly, the cleanup happens only
once, at the next run of the event loop, even though it may be
requested multiple times (both between the first call and the time
it actually runs, as well as after it has already run once). Then
the init part would be:
m_deferredFixupTimer = [[NSTimer alloc] initWithFireDate: [NSDate
distantFuture] interval: 0 target: myObject selector: @selector
(doFixUp:) userInfo: NULL repeats: NO];
[[NSRunLoop currentRunLoop] addTimer: m_deferredFixupTimer
forMode: NSDefaultRunLoopMode];
and then "trigger" part would be:
[m_deferredFixupTimer setFireDate: [NSDate date]];
[m_deferredFixupTimer release];
m_deferedFixupTimer = nil;
After the first time it is triggered, the thing will be nil, so no
problem...
Glenn Andreas email@hidden
<http://www.gandreas.com/> wicked fun!
quadrium | flame : flame fractals & strange attractors : build,
mutate, evolve, animate
_______________________________________________
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