How to prevent NSTimer from delaying or interrupting UI actions in iOS 5

How can I prevent NSTimer from delaying by the user scrolling the table?

I found the answer:

I had a timer that repeated about 8 or 9 times at intervals of 0.4 to 0.8 seconds. I don’t need much precision, but if the user scrolls the table, the timer stops working until the table finishes scrolling (this may take several seconds!). I thought I needed background threads, but the timers on the background threads were a bit complicated to implement.

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

//////////// 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 fine.

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

+5
1

NSDefaultRunLoopMode. UITrackingRunLoopMode ( ).

+1

All Articles