• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

有没有理由保留计划的NSTimer如果你不需要使它无效

用户头像
it1352
帮助3

问题说明

我要回到一些crufty代码整理它,我看到我一直保留从 scheduledTimerWithTimeInterval返回的NSTimers:target:selector:userInfo:repeats:并将它们存储在字段中,然后在触发时释放和取消字段。在某些情况下,我使用计时器向其发送无效的消息 - 但在很多情况下不是。

I'm going back over some crufty code to tidy it up and I see I've been retaining NSTimers returned from scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: and storing them in a field - then releasing and nulling the field when it fires. In some cases I use the timer to send an invalidate message to it later - but in many cases not.

在最近的代码中,我通常只是计划定时器,它。我知道构造函数方法是自动释放,并且定时器在运行循环保持,而它是活动的 - 所以我没有看到任何问题。

In more recent code I usually just schedule the timer and forget about it. I understand that the constructor method is autoreleasing and the timer is retained by the run loop while it is active - so I don't see any issue with this.

只是为了加深我的理解 - 是否有任何其他原因,我应该坚持我的计时器,或者是我现在正在做的接受的成语?

So, just to round out my understanding - is there any other reason I should be holding on to my timers, or is what I am doing now the accepted idiom?

这是所有在iPhone代码的上下文中,但我不相信这是iPhone的具体。

This is all in the context of iPhone code, but I don't believe this is iPhone specific.

正确答案

#1

保留,我 - 这是拥有定时器的运行循环,而不是我。如果你看到我的意思。

I just let the run loop handle the retention, myself - it's the run loop that owns the timer and not me. If you see what I mean.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcakffa
系列文章
更多 icon
同类精品
更多 icon
继续加载