Resolved Scrolling blocks thread?

Discussion in 'iPhone/iPad Programming' started by ArtOfWarfare, Aug 12, 2011.

  1. ArtOfWarfare, Aug 12, 2011
    Last edited: Aug 13, 2011

    macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    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?
     
  2. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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];
    
     
  3. thread starter macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    Thanks! That worked perfectly!
     

Share This Page