nitin tyagi
nitin tyagi

Reputation: 1186

NSTimer in background

I am very new in Iphone Development. I have an issue . I am using a NSTimer which update a UiLabel in every second. now i have two problem :

  1. when my app goes in background and after it when i open app . app goes hangs.
  2. if i goes next or back on other ui screen then wen i comes on timer screen then my label again shows 0.

can anyone help me.

code which i am using :

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

-(void) updateCountdown
{
    secondsLeft--;

    //nits testing
    if(secondsLeft == 1)
    {
         [self.view addSubview:recipePage6View.view];
    }



    if (secondsLeft<0)
    {
        [timer invalidate];
        timer=nil;
        lblDisplayTimer.text =@"00:00:00";

    }
    else
    {
        hours = secondsLeft / 3600;
        minutes = (secondsLeft % 3600) / 60;
        seconds = (secondsLeft %3600) % 60;

        lblDisplayTimer.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
         //lblDisplayTimer.text = [NSString stringWithFormat:@"%02d:%02d",minutes,seconds];
     } 
}

Upvotes: 1

Views: 1337

Answers (1)

David Elliman
David Elliman

Reputation: 1389

You need to set up a special background timer task and not have an NSTimer on the main run loop which gets suspended in background. The documentation on how to do this is here

Upvotes: 4

Related Questions