后台重复作业
我有一个关于 xcode 中多线程的问题。我搜索了许多网站,但仍然无法让我的应用程序运行。即使按下主页按钮,我也想每 5 秒重复一次工作,即继续在后台计时。以下是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
[NSThread detachNewThreadSelector:@selector(test) toTarget:self withObject:nil];
}
在 test.m 中,
-(void)test {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(sayYeah) userInfo:nil repeats:YES];
[pool release];
}
在 sayYeah.m 中,
-(void)sayYeah {
NSLog(@"yeah");
}
我期望的是即使按下主页按钮,“yeah”也会继续每 5 秒弹出一次。但事实并非如此,任何人都可以知道多线程应该如何实现吗?谢谢!
I have a question about multithreading in xcode. I have searched for numerous web sites but still cannot get my app to work. I want to do a repeated job every 5sec even the Home button is pressed i.e. continue to time in background. The following is my code:
- (void)viewDidLoad {
[super viewDidLoad];
[NSThread detachNewThreadSelector:@selector(test) toTarget:self withObject:nil];
}
In test.m,
-(void)test {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(sayYeah) userInfo:nil repeats:YES];
[pool release];
}
in sayYeah.m,
-(void)sayYeah {
NSLog(@"yeah");
}
What I expect is "yeah" will continue to pop up every 5sec even the home button is pressed. But it didnt, can anyone have an idea how the multithread should be implemented? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您的应用属于 VOIP、音频播放或位置更新类别,否则您不能。
而且你不能使用计时器。
You can't unless your app fall is the category VOIP, audio playback or location update.
And you can't use timers.