NSTimer in background

可紊 提交于 2019-12-06 15:02:44

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/18124039/nstimer-in-background

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!