NSTimer在更改后未触发

时间:2013-07-29 12:54:20

标签: ios cocoa nstimer

计划定时更新一些计数器:

-(void)_tock:(NSTimer *)aTimer {
    NSLog(@"%p %p tock from %p", self, [NSThread currentThread], aTimer);
    ...
}

它是从主线程设置并自动添加到runloop(使用schedule而不是timerWith):

-(void)setRotationsPerMinute:(float)rotationsPerMinute
{    
    // force calling from the main thread - as per hint on the invalidate method.

    [self performSelectorOnMainThread:@selector(setRotationsPerMinuteObj:)
                           withObject:[NSNumber numberWithFloat:rotationsPerMinute]
                        waitUntilDone:NO];
}
-(void)setRotationsPerMinuteObj:(NSNumber *)rotationsPerMinuteAsObj
{
    _rotationsPerMinute = [rotationsPerMinuteAsObj floatValue];

    [self.rotationUpdateTimer invalidate];      // <----- invalide timer
    self.rotationUpdateTimer = nil;             // <----- wipe

    ....
    // <--- replace timer
    self.rotationUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:interval
                                                           target:self
                                                              selector:@selector(_tock:)
                                                         userInfo:nil
                                                          repeats:YES];**

    NSLog(@"%p %p New timer is %p - will fire in %f seconds", self, 
        [NSThread currentThread], self.rotationUpdateTimer, interval);

    [self setNeedsDisplay];
}

这最初是花费不大的。然而,当我通过远程推送通知更改它时 - 不知何故它不再触发。

调试输出显示tock很好地触发:

2013-07-29 14:46:21.999 XXX[589:707] 0x1523b0 0x11a5e0 tock from 0x178890
2013-07-29 14:46:22.014 XXX[589:707] 0x1523b0 0x11a5e0 tock from 0x178890
2013-07-29 14:46:22.029 XXX[589:707] 0x1523b0 0x11a5e0 tock from 0x178890
2013-07-29 14:46:22.044 XXX[589:707] 0x1523b0 0x11a5e0 tock from 0x178890
2013-07-29 14:46:22.063 XXX[589:707] 0x1523b0 0x11a5e0 tock from 0x178890

我们接着推进 - 这是一个setRotationsPerMinute:XX电话。

2013-07-29 14:46:22.074 XXX[589:707] Push received: {
    aps =     {
        alert = XXX;
        badge = 1;
        sound = default;
    };
}

作为setRotationsPerMinute:call的结果,我们看到了更改:

2013-07-29 14:46:22.081 XXX[589:707] Makers 1 - active 1 -- 16.000000

然后创建新的计时器:

2013-07-29 14:46:22.089 XXXX[589:707] 0x1523b0 0x11a5e0 New timer is 0x15dbc0 - will fire in 0.060000 seconds

然而 - 它不会从上面开火。任何建议 - 有一种感觉我错过了一些令人目眩的明显的事情!

谢谢!

1 个答案:

答案 0 :(得分:0)

使用Xcode 6.1附带的SDK完全消失了这个问题。