0

I have a timer that fires a method in every 60ms of interval when the application is in foreground and fires in the same interval even if it is in background.

When I initiate/receive a cellular call, the timer fires the method in every 120ms of interval.I thought it is a problem with the timer, so I tried the following approaches.

Approaches I have tried:

  1. NSTimer in background thread.
  2. NSTimer in main thread.
  3. dispatch_source_timer
  4. while loop with 60ms of sleep. (No timer here)

So even if you use a simple while loop, still there is a delay in firing the method. So to maintain the interval I changed the timer interval to 30ms(for all the approaches) when I receive/initiate a call but the result is same(120 ms).

I will be glad if anyone can suggest an approach.

PKN
  • 146
  • 4
  • Why on earth do you need a timer to fire every 60ms in the background? That seems to be the crux of the problem here... – Airsource Ltd Sep 21 '15 at 09:33
  • Dude, I can't tell you why I want a timer to fire every 60ms here. If you have any approach in your mind to fire a method in every 60ms irrespective of the state of the application, please answer. – PKN Sep 21 '15 at 09:41
  • How are you sleeping? NSThread sleepFor... or usleep? – Airsource Ltd Sep 21 '15 at 21:08
  • What happens if you don't sleep at all (on a background thread)? Your processing will still get interrupted eventually, do you get resumed within the requisite 60ms? If not, you're going to struggle - if you do, then a starting point to running every 60ms is "don't sleep at all", and it's a very useful data point. – Airsource Ltd Sep 21 '15 at 21:11
  • I used NSThread sleepForTimeInterval for sleeping – PKN Sep 22 '15 at 09:37

1 Answers1

0

From the Apple Doc:

If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time

What you should remember about Timer is that when you set it to a time T, you have the assurance that the elapsed time between two ticks is at least of T.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • Thank you for the quick response. As I have mentioned It fires the method in every 120ms interval even if you use a simple while loop that calls the designated method in every 60 ms interval. So it is nothing to do with timer, as it slows down the application's runloop when you are in a cellular call. – PKN Sep 21 '15 at 09:32
  • I guess in that simple while loop you are using `[NSThread sleepForTimeInterval:T]`? That method is only guaranteed to sleep for *at least* time interval T. In short, there is no real way on a multitasking system to ensure you sleep for *only* time interval T, only to make sure you sleep for *at least* time interval T. This is because any other task could be scheduled while you were sleeping, and the system could take longer than T to return to your task. – jbg Jan 02 '16 at 09:59