Objective-C NSTimer Tips
April 10, 2009 § Leave a comment
- a timer retains the target and userinfo objects
- a timer is automatically retained by the run loop when scheduling it
- if a timer is not set to repeat, it will automatically invalidate upon firing
- a timer is released from the run loop when calling invalidate
- a timer releases the target and userinfo objects when calling invalidate
If you release a repeating timer without invalidating, it will continue to repeat. The run loop is retaining the timer.
If you don’t want to stop the timer before the application ends, or the timer is non-repeating, you can release it after scheduling it without calling invalidate.
Another option is to use performSelector:withObject:afterDelay:. It is much easier to use.
If you alloc, copy, retain, then you must release the object for memory. Otherwise, you must not release the timer.