iPhone背景线程中的问题

时间:2011-08-25 09:25:50

标签: iphone background-service

我正在使用后台线程来更新我的一个标签

我正在使用以下代码。但是在iOS 4.0中,我已经知道应用程序会保存其状态并转到后台。我的应用程序也做了那项工作,但我正在使用的线程停止工作时,我隐藏应用程序,并再次从我重新打开时离开的地方恢复。任何人都可以告诉我,我需要更改代码,以便让线程继续在后台运行,并在我的应用程序被隐藏时更改我的GUI。我正在使用此代码..

-(void)startThread
{


NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(setUpTimerThread) object:nil];


[thread start];

 }

-(void)setUpTimerThread
{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSTimer *timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(triggerTimer) userInfo:nil repeats:YES];

NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSRunLoopCommonModes];
[runLoop run];
[pool release];

}

-(void)triggerTimer
{

NSLog(@"***Timer Called after 3 seconds*** %d",count);
self.label.text = [NSString stringWithFormat:@"count value = %d",count];
//self.titleLabel.text= [NSString stringWithFormat:@"count value = %d",count];
count = count +1;
}

由于

1 个答案:

答案 0 :(得分:1)

计时器无法重新启动应用程序。您需要做的是从appDelegate的applicationDidBecomeActive:方法重新初始化计时器,并确保从applicationWillEnterBackground关闭计时器:方法