xcode 如何使用 NSTimer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3439952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to work with NSTimer
提问by Joakim B?rjesson
Im using alot of timers in my application. For recording time, moving object, fading etc. I use the same timer for several puposes in the same view at different times. How should I declare and invalidate or release my timers properly?
我在我的应用程序中使用了很多计时器。对于记录时间、移动物体、淡入淡出等。我在不同时间在同一视图中为多个目的使用相同的计时器。我应该如何正确声明和无效或释放我的计时器?
Atm Im declaring the timers like this:
Atm 我像这样声明计时器:
fadeTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(bortInfo) userInfo:nil repeats:YES];
and as soon as im not using it im doing this:
一旦我不使用它,我就会这样做:
[fadeTimer invalidate];
fadeTimer = nil;
The retain count when im leaving the view is 0 on every timer. Should i release the timer in the dealloc aswell? My app runs quite good, but from time to time it crashes.
我离开视图时的保留计数在每个计时器上都是 0。我应该在 dealloc 中释放计时器吗?我的应用程序运行良好,但有时会崩溃。
The clockTimer that i use for updating a label with the time uses
我用于使用时间更新标签的clockTimer
[[NSRunLoop mainRunLoop] addTimer:clockTimer forMode:NSRunLoopCommonModes];
Do i need to do anything with this mainLoop once i invalidate the clockTimer?
一旦我使clockTimer无效,我是否需要对这个mainLoop做任何事情?
All in all please support me with some info about working with timers.
总而言之,请支持我提供一些有关使用计时器的信息。
Thank you very much!
非常感谢!
Joakim
约金
回答by deanWombourne
You're not retaining your timers properly - if you want to refer to them again you should retain them. I'd do this with a property i.e. in your header file
你没有正确保留你的计时器 - 如果你想再次引用它们,你应该保留它们。我会用一个属性来做这个,即在你的头文件中
@property (nonatomic, retain) NSTimer *fadeTimer;
and change your code to say
并更改您的代码以说
self.fadeTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(bortInfo) userInfo:nil repeats:YES];
// Put this whenever you want to remove your timer and in your dealloc method.
[fadeTimer invalidate];
self.fadeTimer = nil;
This will make sure that your timer is retained by your object. Otherwise you just have to hope that the timer stays around and doesn't get autoreleased by the iPhone. And as you say it's crashing occasionally, this might be the reason ;)
这将确保您的计时器被您的对象保留。否则,您只需要希望计时器保持不变并且不会被 iPhone 自动释放。正如您所说,它偶尔会崩溃,这可能是原因;)
I'm afraid I don't know much about run loop but am confused why your don't just use a normal NSTimer
to schedule things - why bother interacting with the run loop at all?
恐怕我对运行循环不太了解,但我很困惑为什么你不只是使用正常NSTimer
来安排事情 - 为什么要与运行循环进行交互呢?
回答by tc.
- Scheduled timers are retained by the run loop, and retain their target. If you want to retain the timer, you have to jump through a few hoops to prevent a retain cycle (I wrote a non-retaining proxy class, which is a bit messy but it works).
- Don't manipulate the run loop unless you know what you're doing (I don't). A "scheduled" timer is already added to the main run loop. If you're generating clockTimer like fadeTimer, then it's being added to the run loop twice.
- "from time to time it crashes" doesn't help anyone. Run it in the debugger and see where it crashes. It might even print some messages to the console if you're lucky.
- 调度的计时器由运行循环保留,并保留它们的目标。如果要保留计时器,则必须跳过几圈以防止保留循环(我写了一个非保留代理类,虽然有点乱,但确实有效)。
- 除非您知道自己在做什么(我不知道),否则不要操作运行循环。一个“预定的”计时器已经添加到主运行循环中。如果您生成像fadeTimer 这样的clockTimer,那么它会被两次添加到运行循环中。
- “不时崩溃”对任何人都没有帮助。在调试器中运行它,看看它在哪里崩溃。如果幸运的话,它甚至可能会向控制台打印一些消息。
回答by Bhavesh Lathigara
*also you can use and this is a better and optimize way to write this line
if (theTimer != nil) {
if([theTimer isValid]){
[theTimer invalidate];
}
theTimer = nil;
}*