5

How can I prevent a NSTimer from being delayed by the user scrolling a table?

I found the answer:

I had a timer that repeated about 8 or 9 times with intervals of 0.4 to 0.8 seconds. I don't need much precision, but if the user scrolls a table the timer would stop working until the table finished scrolling (this could be a few seconds wait!). I thought I needed background threads, but timers on background threads were somewhat complicated to implement.

The answer to my problem was very very simple and easy. I just need to add a line after invoking the timer:

//////////// start the timer

self.playingTimer = [NSTimer scheduledTimerWithTimeInterval:tempo target:self selector:@selector(playSoundFromArray:) userInfo:nil repeats:YES];

//////////// the magic line:

[[NSRunLoop currentRunLoop] addTimer:self.playingTimer forMode:UITrackingRunLoopMode];

Now I can scroll the table as much as I want and my timers work OK!!!

Now I need to study a little more NSRunLoop...

alvira
  • 67
  • 4

1 Answers1

1

You should add your timer for NSDefaultRunLoopMode mode. UITrackingRunLoopMode is used by tracking UI actions(in your case scrolling).

cekisakurek
  • 2,474
  • 2
  • 17
  • 28