Re: How to use NSTimer correctly?
Re: How to use NSTimer correctly?
- Subject: Re: How to use NSTimer correctly?
- From: Dave Keck <email@hidden>
- Date: Sat, 25 Jul 2009 02:48:37 -1000
First off, your object's -dealloc is never going to get called,
because NSTimers retain their targets. (If your -dealloc is getting
called, then you've got some memory management issues.) A breakpoint
or an NSLog in -dealloc will tell you if it's getting called.
> I'm not sure, if I must invalidate the timer, but think I should do it.
I always write the cleanup code even when not strictly necessary, as
is the case with singletons. It helps later if it turns out you need
multiple instances of the object - the cleanup's already there.
Note though that since you didn't retain the timer, it'll be released
as soon as you invalidate it, so it's a good idea to set the ivar to
nil, so any subsequent attempts to access the timer won't crash your
app.
> Here is the onTimer: procedure, where I had an exception after I closed the
> application.
There are many kinds of exceptions. Which one?
> I tried to remove timer invalidation from the dealloc, but
> nothing helped unless I've inserted a boolean var processingTimer, which
> should indicate whether our app still lives or it is slosing now. First I've
> added this var check only at the top of onTimer procedure, yet before the
> main calculating procedure call. That didn't helped. Then I've added yet
> another check right before the call of [NSTable reloadData]. And this
> helped. At least I've tried to run and close my app several times and I
> didn't see an exception.
This is getting kludgy, which is usually a sign that you're doing
something wrong. Treat the source of the problem, not the symptom!
What exception did you get?
> Also I tried to change timer interval, believing that my main procedure runs
> too slowly, so overlapped onTimer calls occur. But it makes no sense.
Since this code is presumably single-threaded, it's impossible that
one timer invocation could overlap another.
> So, what is the _correct_ way to do all that?
For this situation, a good place to stick your cleanup code would be
the NSApp's delegate method -applicationWillTerminate:, or you could
observe the respective notification.
David
_______________________________________________
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