Background thread problem on iPhone

I am using Background Thread to update one of my shortcuts

I am using the following code. But in iOS 4.0 I found out that the app saves its states and goes into the background. and my app was doing this job as well, but the thread I am using stops working when I hide the app, and resumes again from where I left when I reopen it. Can someone please tell me what I need to change in the code so that the thread keeps running in the background and changing my GUI while my app is hidden. I am using this code.

-(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;
}

      

thank

0


source to share


1 answer


Timers will not work when you restart the application. What you need to do is reinitialize the timer from your app appDelegate applicationDidBecomeActive: and make sure you disable the timer from your app WillEnterBackground: method



+1


source







All Articles