开发者

When setting up a timed execution using NSDate, NSTimer, and NSRunLoop, which one do I need to not release before the action in complete?

Here's the part of the code dealing with it:

NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:1.0+index];
        NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate
                                                    interval:0.5
                                                    target:self
                                                  selector:@selector(countedtargetMethodGlow:)
                                                  userInfo:nil
                                                   repeats:NO];

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

But it's in a loop, so I'll be making a buncha these, and 开发者_运维技巧I don't know what I need to leave alone for the firing not to be messed up.


Every timer object you add to a run loop is retained by the run loop until it's invalidated (effectively indicating that the run loop is taking "ownership" of the timer while it needs it). So you can release any of those timers without affecting how they're scheduled on the run loop. If you need them for some independent purpose you should not release them, so they're guaranteed to still be around for you even if the run loop has finished with them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜