目标 C:使用计时器时应用程序冻结

发布于 2024-10-09 07:31:55 字数 880 浏览 0 评论 0原文

我花了几个小时才弄清楚如何在我的程序中实现计时器,但是当它运行时,应用程序并没有像计时器之前那样完全加载。

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

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

发布评论

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

评论(3

饮湿 2024-10-16 07:31:55

有几件事:

在使用 [NSTimer ScheduledTimerWithTimeInterval:] 设置一个计时器后,您不需要自动释放返回的计时器,它已经自动释放了。

通过 ScheduledTimerWithInterval 创建的计时器已添加到默认运行循环中。所以你不需要使用以下内容:

[[NSRunLoop currentRunLoop] addTimer: theTimer forMode: NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];

事实上,你甚至不需要保留对计时器的引用,除非你需要自己取消它。

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:

[[NSRunLoop currentRunLoop] addTimer: theTimer forMode: NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];

In fact, you don't even need to keep a reference to the timer unless you need to cancel it yourself.

薄暮涼年 2024-10-16 07:31:55

以下是苹果对您所做的事情的说明 在文档中

运行

将接收器置于永久状态
循环,在此期间它处理
来自所有附加输入源的数据。

  • (void)run 讨论 如果没有输入源或计时器附加到
    run循环,该方法退出
    立即地;否则,它运行
    NSDefaultRunLoopMode 中的接收器
    通过反复调用
    运行模式:日期之前:。换句话说,
    该方法有效地开始了
    无限循环处理数据
    运行循环的输入源和
    计时器。

手动删除所有已知输入
运行循环中的源和计时器
并不能保证运行循环
将退出。 Mac OS X 可以安装并
删除额外的输入源
需要处理针对以下目标的请求
接收者的线程。那些来源
因此可以防止运行循环
退出。

如果您希望运行循环终止,
你不应该使用这个方法。
相反,使用其他运行之一
方法并检查其他任意
你自己的条件,循环。一个
简单的例子是:

BOOL 应该保持运行 = YES;
// 全局 NSRunLoop *theRL =
[NSRunLoop currentRunLoop];尽管
(shouldKeepRunning && [theRL
运行模式:NSDefaultRunLoopMode
beforeDate:[NSDate distanceFuture]]);
其中 shouldKeepRunning 设置为 NO
程序中的其他地方。

适用于 iOS 2.0 和
稍后。

所以看起来你的代码正在做它应该做的事情。它记录所有计时器事件并无限期地等待运行循环。

Here is what apple has to say about what you are doing in the documentation

run

Puts the receiver into a permanent
loop, during which time it processes
data from all attached input sources.

  • (void)run Discussion If no input sources or timers are attached to the
    run loop, this method exits
    immediately; otherwise, it runs the
    receiver in the NSDefaultRunLoopMode
    by repeatedly invoking
    runMode:beforeDate:. In other words,
    this method effectively begins an
    infinite loop that processes data from
    the run loop’s input sources and
    timers.

Manually removing all known input
sources and timers from the run loop
is not a guarantee that the run loop
will exit. Mac OS X can install and
remove additional input sources as
needed to process requests targeted at
the receiver’s thread. Those sources
could therefore prevent the run loop
from exiting.

If you want the run loop to terminate,
you shouldn't use this method.
Instead, use one of the other run
methods and also check other arbitrary
conditions of your own, in a loop. A
simple example would be:

BOOL shouldKeepRunning = YES;
// global NSRunLoop *theRL =
[NSRunLoop currentRunLoop]; while
(shouldKeepRunning && [theRL
runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture]]);
where shouldKeepRunning is set to NO
somewhere else in the program.

Availability Available in iOS 2.0 and
later.

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.

单调的奢华 2024-10-16 07:31:55

看来你让它变得比需要的复杂得多。您不需要将任何代码放入 main.m 文件中。如果您想每秒触发 doStuff 方法,这就是您需要的全部代码:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector: @selector(doStuff) userInfo:nil repeats:YES];

您不需要自己(自动)释放它。 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:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector: @selector(doStuff) userInfo:nil repeats:YES];

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 call invalidate and set the reference to nil.

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