我希望我的计时器在后台运行,并且我想真正做到这一点的唯一方法是节省appliationDidEnterBackground的时间并用applicationDidLaunchWithOptions.检索它。我的计时器使用Core Data和timeInterval(testTask.timeInterval)每次递减后都保存为:

-(IBAction)startTimer:(id)sender{
    if (timer == nil) {
        [startButton setTitle:@"Pause" forState:UIControlStateNormal];
       timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    } else {
        [startButton setTitle:@"Resume" forState:UIControlStateNormal];
        [timer invalidate];
        timer = nil;
    }

}
-(void)timerAction:(NSTimer *)t
{
    if(testTask.timeInterval == 0)
    {
        if (self.timer)
        {
            [self timerExpired];
            [self.timer invalidate];
            self.timer = nil;
        }
    }
    else
    {
        testTask.timeInterval--;
        NSError *error;
        if (![self.context save:&error]) {
            NSLog(@"couldn't save: %@", [error localizedDescription]);
        }
    }
    NSUInteger seconds = (NSUInteger)round(testTask.timeInterval);
    NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
                        seconds / 3600, (seconds / 60) % 60, seconds % 60];
    timerLabel.text = string;
    NSLog(@"%f", testTask.timeInterval);
}
-(void)timerExpired{
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.alertBody = @"Time is up";
    localNotification.alertAction = @"Ok";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication]presentLocalNotificationNow:localNotification];
}


这些方法位于详细视图控制器中,该控制器通过以下方式初始化:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *detailVC;
    if (![self.detailViewsDictionary.allKeys containsObject:indexPath]){
        detailVC = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
        [self.detailViewsDictionary setObject:detailVC forKey:indexPath];
        detailVC.context = self.managedObjectContext;
    }else{
        detailVC = self.detailViewsDictionary[indexPath];
    }
        Tasks *task = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        detailVC.testTask = task;
        [[self navigationController] pushViewController:detailVC animated:YES];
    NSLog(@"%@", self.detailViewsDictionary);
}


我不明白的是...我将如何访问timeInterval(每个detailviewcontroller具有不同的timeinterval ...),以便可以将其放在appliationDidEnterBackground中?另外,我猜我应该节省应用程序进入后台的时间,然后保存进入前台的时间,然后减去?然后我从时间间隔中减去该值,对吗?

最佳答案

首先,如果您完全关心准确性,则不应使用像这样的直接倒计时。 NSTimer不会以精确的一秒间隔触发,并且延迟会累积。更好的方法是在计时器启动时创建一个NSDate,然后从每个滴答声开始获取NSTimeInterval,然后计算分钟和秒。

为了使每个视图控制器存储自己的开始时间,您可以注册任何对象以接收UIApplicationDidEnterBackgroundNotification。这是在应用程序委托获取applicationDidEnterBackground:的同时发布的。


  另外,我猜我应该节省应用程序进入后台的时间,然后保存进入前台的时间,然后减去?然后我从时间间隔中减去该值,对吗?


是:

NSTimeInterval idleTime = [dateReturnedToForeground timeIntervalSinceDate:dateEnteredBackground];
NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:startDate];
elapsedTime -= idleTime;


将为您提供计时器的活动时间。

10-04 11:57
查看更多