Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent an NSTimer to be delayed or interrupted by user interface actions in iOS 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...

like image 245
alvira Avatar asked Nov 14 '22 05:11

alvira


1 Answers

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

like image 156
cekisakurek Avatar answered May 16 '23 08:05

cekisakurek