当iOS应用程序转到后台时NSTimer更新间隔发生变化?
问题描述:
我记录了CMDeviceMotion
加速度数据的样本,持续30秒到NSTimer
。事情是,当应用程序在前台,一切都很好。间隔设置为0.01
,并在30 seconds
,3000
中存储读数。但是当应用程序进入后台时,我只能得到300
的读数。当iOS应用程序转到后台时NSTimer更新间隔发生变化?
self.deviceMotionTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(recordUpdates) userInfo:nil repeats:YES];
NSTimer是如何工作的?
答
NSTimer
将永远不会在后台工作。当你的应用程序进入后台时,NSTimer也会冻结。总之,当你想在后台执行一些重复任务时,你不能使用NSTimer。
如果你仍然想在后台运行的东西,然后使用这个线程通过@Robert给出了答案: Scheduled NSTimer when app is in background?