Re: NSTimer crashes after invalidation
Re: NSTimer crashes after invalidation
- Subject: Re: NSTimer crashes after invalidation
- From: j o a r <email@hidden>
- Date: Wed, 16 Feb 2005 01:03:47 +0100
On 2005-02-16, at 00.55, Michael Becker wrote:
Here's the scenario: The user operates an NSSlider. Now whenever the
slider has been moved, an NSTimer is triggered to perform a certain
(lengthy) operation. When the user drags the slider, there will be
many timers triggered (the slider is set to continuously send action
messages).
Here is my plan: To avoid the many timers, I set the timer to fire in
0.2 secs. In my action method, I need to check if a timer is currently
triggered. If that is the case, I invalidate that timer and set a new
one up. This way, I can make sure that the timer fires only once after
the user dragged the slider.
Sounds a little confusing (even to me, now). But it works. And that's
why I need to keep a variable to the timer -- I want to check it and
maybe invalidate it BEFORE it fired.
Here's an easier way to do that same thing without having to manage a
NSTimer object:
- (void) delayedSliderAction:(id) sender
{
// Start lengthy operation
}
- (IBAction) sliderAction:(id) sender
{
[[self class] cancelPreviousPerformRequestsWithTarget: self];
[self performSelector: @selector(delayedSliderAction:) withObject:
sender afterDelay: 0.2];
}
Documentation for the "performXXX" methods can be found in the ref.
docs. for NSObject.
One thing to keep in mind here: Both "self" and "sender" are retained
until the delayed action is performed. This can cause problems for
example if the window is closed before the delayed action is triggered.
That said, if you just keep this in mind, it's of course something
that's really easy to protect against.
j o a r
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden