Re: Invalidate non-repeating NSTimer after fired?
Re: Invalidate non-repeating NSTimer after fired?
- Subject: Re: Invalidate non-repeating NSTimer after fired?
- From: Trygve Inda <email@hidden>
- Date: Fri, 03 Aug 2012 14:37:10 -0700
- Thread-topic: Invalidate non-repeating NSTimer after fired?
> You must use the 2nd way. If you don't you could get a memory exception if the
> timer has fired and, therefore, been invalidated.
>
> On Aug 3, 2012, at 4:19 PM, Trygve Inda wrote:
My code looks like this:
-(void)awakeFromNib
{
[self setUpdateTimer:[NSTimer scheduledTimerWithTimeInterval:kSomeValue
target:self selector:@selector(wantsUpdate:) userInfo:nil repeats:NO]];
}
-(void)wantsUpdate:(NSTimer *)inTimer
{
// do stuff
// conditionally do
[self setUpdateTimer:[NSTimer scheduledTimerWithTimeInterval:kSomeValue
target:self selector:@selector(wantsUpdate:) userInfo:nil repeats:NO]];
}
-(void)setUpdateTimer:(NSTimer *)inTimer
{
if (updateTimer)
[updateTimer invalidate];
[updateTimer release];
updateTimer = nil;
}
updateTimer = [inTimer retain];
}
setUpdateTimer can be called in three different ways...
A: From awakeFromNib where updateTimer is of course nil and the new timer
gets retained. All is ok here.
B: From wantsUpdate: In this case we are being called from within the method
triggered by the timer and thus the code has not yet fallen back into the
run loop so updateTimer is true and [updateTimer isValid] is YES, and once
we fall back to the run loop the timer will be invalidated.
C: From somewhere else in the program. In this case updateTimer is true, but
[updateTimer isValid] is NO since the code has already fallen back into the
run loop and invalidated the timer.
It needs to work in all three case but I think B is the concern since if I
change the code to:
if ([updateTimer isValid])
[updateTimer invalidate];
Then it will be invalidated in case B (because the code has not fallen back
to the run loop to invalidate it on its own). Not sure if I should be
invalidating it after it has fired in this case...before the system does
How best to handle all three cases elegantly?
_______________________________________________
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