创建多个动态NSTimers,在第一次加载时不重复,之后很好

时间:2013-04-03 10:19:33

标签: iphone objective-c xcode nstimer

我使用以下代码从存储在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];
    }
}

1 个答案:

答案 0 :(得分:1)

如果在使用scheduledTimerWithTimeInterval创建NSTimer后没有触发,通常意味着它尚未附加到正确的runloop。可能是你的方法setupNotificationTimer()在初始化期间和返回前台时使用不同的runloops调用? (即在不同的线索或类似的情况下)

此代码可解决此问题:

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

这可确保您的计时器安排在主跑道上并开火。