alex willrock
alex willrock

Reputation: 996

NSTimer with UITableView

I have UIView with button Start, UILabel with my NSTimer time and UITableView with time record.

Problem is when I scroll my tableview - NSTimer freeze, why I can do to they work async?

My code:

- (IBAction)startTimer {

_startButton.hidden = YES;
_stopButton.hidden = NO;

isRun = YES;

UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
}];

myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01f
                                           target:self
                                         selector:@selector(tick)
                                         userInfo:nil
                                          repeats:YES];
}

- (void)tick {

    _timeSot++;

    sot++;
/*some code*/

    myTime = [NSString stringWithFormat:@"%@:%@:%@", minStr, secStr, sotStr];


    [_timeLabel setText:myTime]; 
}

Upvotes: 2

Views: 354

Answers (1)

simalone
simalone

Reputation: 2768

Add myTimer to NSRunLoopCommonModes:

myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01f
                                           target:self
                                         selector:@selector(tick)
                                         userInfo:nil
                                          repeats:YES];

 [[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSRunLoopCommonModes];

Upvotes: 2

Related Questions