iphone - what is the first step in (NSTimer release and invalidate)? -


Can I send a argument to @selector in NSTimer ? If I want to release NSTimer , then what are the following steps in dealloc

  [invalid timer]; [Timer release];  

[timer release] / P>

Because the run loop maintains the timer, since the management of the memory management generally does not need to maintain the reference of the timer, since the timer is passed as an argument When you specify your method in the form of selectors, then you can choose the appropriate repeating timer within that method. If you can. In many situations, you also want the option to invalidate the timer, perhaps even before it starts. In this case, you need to keep the context of the timer so that you can send an invalid message whenever it is appropriate. If you make an undefined timer (see), then you have to maintain a strong context in the context of the timer (in the context enumerated environment, you can retain it) so that it can not be canceled before using it.

What does this mean?

If you have alloc and init a timer, you should also release , such as:

< Previous> NSTimer * timer = [[Enestimer Elok] Init with ...]; NSRunLoop * runLoop = [NSRunLoop currentRunLoop]; [Run Loop Aid Timer: Timer Formad: NSDefault Array loop mode]; [Timer release]; ... ... [invalid timer]; Timer = zero;

Once the timer has been added to the running loop, there is no reason to keep any reference on it, because Run is the owner of the loop in this case, as shown in As soon as you add it to the run loop, and when you are finished, the will be released as invalid as the last line ( Zero setting timer) is for security. The caller will be released as a result of invalid (by the run loop), so it is unsafe to keep reference which indicates it. Setting the local context of zero does anything.

However, if you create a timer by using the feature methods in a way:

  NSTimer * timer = [NSTimer scheduled timer with time interval ...];  

You not need to call [timer release] ! The feature method adds a timer to the run loop, which is then the owner, so you do not need to manage any memory on the returned timer object. When you no longer want to use it, you will simply remove the invalid timer:

  [timer invalid]; Timer = zero;  

Or, if the timer was not set to repeat, then you will not do anything completely, because it will be released after the first silence.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -