目标 C:使用计时器时应用程序冻结
我花了几个小时才弄清楚如何在我的程序中实现计时器,但是当它运行时,应用程序并没有像计时器之前那样完全加载。
在我的 main.m 中:
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
OutLauncher *theLauncher = [[OutLauncher alloc] init];
NSTimer *theTimer = [theLauncher getTimer];
[theTimer retain];
[[NSRunLoop currentRunLoop] addTimer: theTimer forMode: NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
[pool release];
return 0;
}
文件 OutLauncher 被导入其中,如下所示:
- (void)doStuff {
NSLog( @"Doing Stuff");
}
- (NSTimer *)getTimer{
NSTimer *theTimer;
theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector: @selector(doStuff) userInfo:nil repeats:YES];
return [theTimer autorelease];
}
计时器工作,控制台每秒更新一次短语“doing stuff”,但程序的其余部分不会加载。如果我注释掉添加到 int main 的代码,就会这样
It took me hours to figure out how to implement a timer into my program, but when it runs, the app doesn't load completely as it did before the timer.
In my main.m:
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
OutLauncher *theLauncher = [[OutLauncher alloc] init];
NSTimer *theTimer = [theLauncher getTimer];
[theTimer retain];
[[NSRunLoop currentRunLoop] addTimer: theTimer forMode: NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
[pool release];
return 0;
}
The file OutLauncher is being imported into that, which looks like this:
- (void)doStuff {
NSLog( @"Doing Stuff");
}
- (NSTimer *)getTimer{
NSTimer *theTimer;
theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector: @selector(doStuff) userInfo:nil repeats:YES];
return [theTimer autorelease];
}
The timer works, the console updates every second with the phrase "doing stuff" but the rest of the program just won't load. It will if I comment out the code I added to int main though
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有几件事:
在使用 [NSTimer ScheduledTimerWithTimeInterval:] 设置一个计时器后,您不需要自动释放返回的计时器,它已经自动释放了。
通过 ScheduledTimerWithInterval 创建的计时器已添加到默认运行循环中。所以你不需要使用以下内容:
事实上,你甚至不需要保留对计时器的引用,除非你需要自己取消它。
A few things:
You don't need to autorelease the timer you return after setting one up with [NSTimer scheduledTimerWithTimeInterval:] It is already autoreleased.
The timer created via scheduledTimerWithInterval is already added to the default run loop. So you don't need to use the following:
In fact, you don't even need to keep a reference to the timer unless you need to cancel it yourself.
以下是苹果对您所做的事情的说明 在文档中
所以看起来你的代码正在做它应该做的事情。它记录所有计时器事件并无限期地等待运行循环。
Here is what apple has to say about what you are doing in the documentation
So it looks like your code is doing what it is supposed to do. It is Logging all the timer events and waiting indefinitely for the run loop.
看来你让它变得比需要的复杂得多。您不需要将任何代码放入 main.m 文件中。如果您想每秒触发
doStuff
方法,这就是您需要的全部代码:您不需要自己(自动)释放它。
timer
已自动释放。如果您希望能够取消计时器,则需要保留它的引用。然后,当您想取消时,只需调用invalidate
并将引用设置为nil
即可。It looks like you're making it a lot more complicated than it needs to be. You don't need to put any code in your main.m file. If you want to fire the
doStuff
method every second, this is all the code you need:You don't need to (auto)release it yourself.
timer
is already autoreleased. If you want to be able to cancel the timer, you will need to keep a reference of it. Then when you want to cancel, you just callinvalidate
and set the reference tonil
.