What is the first step (release of NSTimer and cancellation)?

Is it possible to send an argument from @selectorto NSTimer? If I want to free NSTimer, follow these steps right in dealloc?

[timer invalidate];
[timer release];
+5
source share
4 answers

[timer release]you only need to call if you "own" a timer. From the Apple Documentation:

, , , , . , , . , , . , , . (. " " ), ( , ), , .

?

alloc init , release :

NSTimer * timer = [[NSTimer alloc] initWith...];

NSRunLoop * runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
[timer release];

...
...

[timer invalidate];
timer = nil;

, , . , , release , , invalidate, . ( nil) . invalidate , ( ), , . nil .

, , , , :

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval ...];

[timer release]! , , . invalidate , :

[timer invalidate];
timer = nil;

, , , .

+15

. ( ), . , , . , (, , , ).

+8

Always, release is the last thing you do. As soon as you release something, there is no guarantee that you can safely dereference an object, which means that it is safer for sending ANY messages.

+1
source

This is the right way to free a timer that can still work (and you want to stop).

0
source

All Articles