0

我正在使用以下代码从存储在 SQLite 数据库中的数据创建一个数字(~5)NSTimer 实例:

-(void)setupNotificationTimer:(NSDictionary *)timerDetails{

    NSLog(@"TIMER REPEATS VALUE: %@", [timerDetails objectForKey:@"repeats"]);
    NSLog(@"INTERVAL: %f", [[timerDetails objectForKey:@"interval"] floatValue]);

    bool repeat = [[timerDetails objectForKey:@"repeats"] boolValue];
    if (repeat) {
        NSLog(@"Should Repeat");
    }
    else{
        NSLog(@"Should Not Repeat");
    }

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:[[timerDetails objectForKey:@"interval"] floatValue]
                                     target:self
                                   selector:@selector(fireTimer:)
                                   userInfo:timerDetails
                                    repeats:repeat];
    [timer fire];
    [timers addObject:timer]; //'timers' is a property of the AppDelegate
}

-(void)fireTimer:(NSTimer *)timer {
    NSLog(@"Timer Fired");
    NSMutableDictionary *dict = [timer userInfo];
    [[NSNotificationCenter defaultCenter] postNotificationName:[dict objectForKey:@"notificationName"] object:nil];
}

当我第一次设置这些计时器时,应用程序加载计时器时设置正确,如下面的 NSLog 输出所示,但尽管明显设置为重复,但不会重复。每个计时器触发一次然后不会继续。

2013-04-03 11:12:53.541 Test[2635:752b] TIMER REPEATS VALUE: 1
2013-04-03 11:12:53.542 Test[2635:752b] INTERVAL: 10.000000
2013-04-03 11:12:53.543 Test[2635:752b] Should Repeat
2013-04-03 11:12:53.544 Test[2635:752b] Timer Fired

当我关闭应用程序然后重新打开它时,会发生奇怪的事情,计时器被创建并实际上重复触发。这个应用程序也是一个通用的 iPhone 和 iPad 应用程序,我似乎只有在 iPhone 上运行该应用程序时才会出现这种行为。我真的被这件事难住了,任何帮助都将不胜感激。

编辑

根据要求取消计时器的代码:

-(void)viewWillDisappear:(BOOL)animated{        
    NSArray *viewControllersSet = self.navigationController.viewControllers;
    if (viewControllersSet.count > 1 && [viewControllersSet objectAtIndex:viewControllersSet.count-2] == self) {
        // View is disappearing because a new view controller was pushed onto the stack
        NSLog(@"New view controller was pushed");
    } else if ([viewControllersSet indexOfObject:self] == NSNotFound) {
        // View is disappearing because it was popped from the stack
        NSLog(@"View controller was popped");
        for(int i = 0; i < [[appDelegate timers] count]; i ++){
            NSTimer *timer = [[appDelegate timers] objectAtIndex:i];
            [timer invalidate];
            timer = nil;
        }
        [[appDelegate timers] removeAllObjects];
    }
}
4

1 回答 1

1

如果 NSTimer 在使用 scheduleTimerWithTimeInterval 创建后没有触发,通常意味着它没有附加到正确的 runloop。可能是在初始化期间和返回前台时使用不同的运行循环调用了您的方法 setupNotificationTimer() 吗?(即在不同线程或类似的上下文中)

这段代码可以解决这个问题:

NSTimer* timer = [NSTimer timerWithTimeInterval:[[timerDetails objectForKey:@"interval"] floatValue]
                                 target:self
                               selector:@selector(fireTimer:)
                               userInfo:timerDetails
                                repeats:repeat];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

这可确保您的计时器被安排在主运行循环上并触发。

于 2013-04-03T11:27:07.320 回答