Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTimer periodic task doesn't get called while scrolling

I have an NSTimer

timer = [NSTimer scheduledTimerWithTimeInterval:1 
                                         target:self 
                                       selector:@selector(periodicTimer) 
                                       userInfo:nil 
                                        repeats:YES];

which does

- (void)periodicTimer
{
    NSLog(@"Bang!");
    if (timerStart != nil)
        [timerLabel setText:[[NSDate date] timeDifference:timerStart]];        
}

The problem is that while scrolling a tableview (or doing other tasks) the label doesn't get updated, furthermore, "Bang!" doesn't appear, so I supposed the method doesn't get called.

My question is how to update the label periodically even when the user is playing around with the app interface.

like image 818
Fr4ncis Avatar asked May 26 '12 10:05

Fr4ncis


Video Answer


1 Answers

You'll need to add your timer to the UITrackingRunLoopMode to make sure your timer also fires during scrolling.

NSRunLoop *runloop = [NSRunLoop currentRunLoop];
NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(myTimerAction:) userInfo:nil repeats:YES];
[runloop addTimer:timer forMode:NSRunLoopCommonModes];
[runloop addTimer:timer forMode:UITrackingRunLoopMode];

From: https://stackoverflow.com/a/1997018/474896

like image 138
Joris Kluivers Avatar answered Nov 15 '22 19:11

Joris Kluivers