当使用 NSDate、NSTimer 和 NSRunLoop 设置定时执行时,在操作完成之前我需要不释放哪一个?
这是处理它的代码部分:
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];
但它是在一个循环中,所以我将制作这些,我不知道我需要留下什么才能让射击不被搞乱。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加到运行循环的每个计时器对象都由运行循环保留,直到其失效为止(有效地表明运行循环在需要计时器时正在获取计时器的“所有权”)。因此,您可以释放任何这些计时器,而不会影响它们在运行循环上的调度方式。如果您出于某些独立目的需要它们,则不应释放它们,因此即使运行循环已完成它们,它们也保证仍然存在。
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.