NSTimer update interval changes when app goes to background in iOS app?

58 views Asked by At

I was recording a sample of CMDeviceMotion acceleration data for 30 seconds through NSTimer. The thing is, when the app is in foreground, everything is fine. The interval is set to 0.01 and in 30 seconds, 3000 readings are stored. But when app goes to background, I get 300 only readings.

self.deviceMotionTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(recordUpdates) userInfo:nil repeats:YES];

Is this how NSTimer works?

1

There are 1 answers

0
Mehul Thakkar On

NSTimer will never work in background. When your app goes to background, NSTimer also freezes. In short, You can't use NSTimer, when you want to do some repetitive task in background.

If you still want to run something in background, then use the answer given by @Robert in this thread: Scheduled NSTimer when app is in background?