简体   繁体   中英

NSTimer not firing after a change

A timer is scheduled to update some counters:

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

it is set from the main thread and added to the runloop automatically (using schedule rather than 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];
}

This works spendidly initially. However when I change it as the result of a Remote push notification coming it - somehow it does no longer fire.

Debug output shows the tock firing nicely:

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

We then get a push coming in - which does a setRotationsPerMinute:XX call.

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

As a result of the setRotationsPerMinute: call we see the change:

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

and then the creation of the new timer:

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

However - it does not fire from thereon. Any suggestions - have a feeling I am missing something blindingly obvious !

Thanks !

Xcode 6.1随附的SDK完全消除了此问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM