Re: How to properly create and destroy NSTimers?
Re: How to properly create and destroy NSTimers?
- Subject: Re: How to properly create and destroy NSTimers?
- From: Nathan Day <email@hidden>
- Date: Mon, 1 Jul 2002 20:33:28 +0930
On Monday, July 1, 2002, at 07:17 PM, Pierre-Olivier Latour wrote:
1) according the Cocoa standard, when creating a NSTimer with a method
like
"scheduledTimerWithTimeInterval", it should return an autoreleased
object,
which would be released at the end of the current event processing. But
the
timer is going to fire later than this moment. So is the timer an
autorelease object or not? If repeat is OFF, is it going to autorelease
automatically when it has fired?
The documentation doesn't say anything about an autoreleased instance,
this seems to be a common misconception that all class methods always
return autoreleased objects, this is not always the case the class could
be still want to use the instance itself, but you should not depend on
that either way, if you want keep a reference to it then you have to
retain it. Autoreleasing is simply a means to give you a chance to
retain it if the class that returns the instance does not want to hold
onto it anymore itself. The NSTimer is probably autoreleased and then
retain by the NSRunLoop, the instance will probably then be released by
the NSRunLoop when its finished firing but the instance can still exist
if you have retained it also, which you should do if you want to do
anything to it later, like invalidate it.
2) The doc says:
"There is no method that removes the association of a timer from an
NSRunLoopsend the timer the invalidate message instead. invalidate
disables
the timer, so it no longer affects the NSRunLoop."
This is not really clear: when I call "invalidate", does it disable the
timer and I can reschedule it later (how?) or does it destroy the timer?
When the "invalidate" message is sent, is the timer object released or
to I
have to send a "release" message next?
This is a bit confusing, it might be worded that way because some people
may want to stop a timer by sending is a release message, if you have
retained it then this will have no effect since the NSRunLoop has
probable retain it as well. It's up to the NSRunLoop to remove the
NSTimer, you can only tell the NSTimer not to fire anymore or not at all
if it hasn't fired yet. The NSRunLoop might check that a NSTimer is
still valid before it fires it, if it is not it then releases it, so the
NSTImer doesn't get released until it was due to fire, this is just a
guess.
3) Is it safe to invalidate a timer before it has even fired once?
Yes
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.