In my app, I have NSTimer which updates UILabel every second. I've added UIBackgroundTaskIdentifier before NSTimer to let it run in background.
__block UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:updateTimer forMode:NSRunLoopCommonModes];
. . .
-(void)updateTime
{
secondsLeft -= 1;
//NSLog(@"Update time : %d ......",secondsLeft);
dispatch_async(dispatch_get_main_queue(), ^{
_timeLbl.text = [NSString stringWithFormat:@"%d",secondsLeft];
});
}
Problem is when we press home button app gets into background, timer is running but uilabel is not updating. E.g. Say, _timeLbl displaying '45'. Now if I press home button, app goes into background. After 10 sec. I click app icon to get app in foreground, and there I see '45' for a while (fraction of second) and then it display 35, instead of directly display 35. That means label is not getting updated with NSTimer. Is there any workaround to solve this ?
Thanks !