有没有理由保留一个计划的NSTimer如果你不需要使它无效? [英] Is there any reason to retain a scheduled NSTimer if you don't need to invalidate it?

查看:81
本文介绍了有没有理由保留一个计划的NSTimer如果你不需要使它无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要回到一些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.

推荐答案

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

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.

这篇关于有没有理由保留一个计划的NSTimer如果你不需要使它无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆