PDA

View Full Version : [Resolved] Scrolling blocks thread?




ArtOfWarfare
Aug 13, 2011, 12:43 AM
I have a UILabel that informs the user how much time is left in a round of my game. Here's the code:

startDate = [[NSDate alloc] init];
updateClockTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateClock) userInfo:nil repeats:YES];
- (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?



jiminaus
Aug 13, 2011, 01:14 AM
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.


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

ArtOfWarfare
Aug 13, 2011, 09:26 AM
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.


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


Thanks! That worked perfectly!