当使用 NSDate、NSTimer 和 NSRunLoop 设置定时执行时,在操作完成之前我需要不释放哪一个?

发布于 2024-09-02 02:32:26 字数 702 浏览 5 评论 0原文

这是处理它的代码部分:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水溶 2024-09-09 02:32:26

添加到运行循环的每个计时器对象都由运行循环保留,直到其失效为止(有效地表明运行循环在需要计时器时正在获取计时器的“所有权”)。因此,您可以释放任何这些计时器,而不会影响它们在运行循环上的调度方式。如果您出于某些独立目的需要它们,则不应释放它们,因此即使运行循环已完成它们,它们也保证仍然存在。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文