Objective-C NSTimer Tips

April 10, 2009 § Leave a comment

NSTimer

  • 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Objective-C NSTimer Tips at Web Builders.

meta

%d bloggers like this: