Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,706
6,289
I have a UILabel that informs the user how much time is left in a round of my game. Here's the code:

Code:
startDate = [[NSDate alloc] init];
updateClockTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateClock) userInfo:nil repeats:YES];
Code:
- (void)updateClock
{
    double totalSecs = [startDate timeIntervalSinceNow] + SECS_PER_ROUND;
    int mins = totalSecs/60;
    int secs = (int)totalSecs%60;
    int deca = (int)(totalSecs * 10)%10;
    rightScore.text = [NSString stringWithFormat:@"%i:%.2i.%i", mins, secs, deca];
}

It normally displays and updates properly, except when a UIScrollView is scrolling. I suspect the action of scrolling is somehow blocking the main thread, but I'm not really sure as I don't have a particularly firm grasp on understanding how threading works.

Could someone offer me a solution for how to make my countdown timer to continue updating even when scrolling?
 
Last edited:
While scrolling, the main run loop moves out of the default mode. You can try manually adding your NSTimer to the main NSRunLoop so it fires in all modes.

Code:
updateClockTimer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(updateClock) userInfo:NULL repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:updateClockTimer forMode:NSRunLoopCommonModes];
 
While scrolling, the main run loop moves out of the default mode. You can try manually adding your NSTimer to the main NSRunLoop so it fires in all modes.

Code:
updateClockTimer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(updateClock) userInfo:NULL repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:updateClockTimer forMode:NSRunLoopCommonModes];

Thanks! That worked perfectly!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.