2

I want to be able to turn device motion updates on and off during my game. But after I turn them off and then back again, .deviceMotion property of CMMotionManager returns the last know deviceMotion for a short while (around 0.5 seconds, until it can calculate the fresh value, I guess).

So, I want to be able to test the age of the CMDeviceMotion objects I get, and ignore them if they're older than a few seconds.

Calculating CACurrentMediaTime() - deviceMotion.timestamp seems to give me accurate results.. But I'm worried that this might break in a future device or iOS version. Are device motion timestamps guaranteed to remain comparable with CACurrentMediaTime() in the future? Is there a better way to calculate the age of CMDeviceMotion timestamps?

From Apple's documentation:

CACurrentMediaTime()

A CFTimeInterval derived by calling mach_absolute_time() and converting the result to seconds.

deviceMotion.timestamp

The time stamp is the amount of time in seconds since the phone booted.

baris
  • 304
  • 2
  • 14

1 Answers1

1

I had the same problem and needed to keep a history of events. I solved this by setting the updateInterval to pretty low values e.g. 0.5. Thus Core Motion doesn't need to reinitialise the sensors and you slow down energy consumption.

Kay
  • 12,918
  • 4
  • 55
  • 77
  • I'm not marking this as the accepted answer because it doesn't solve the exact question, but I'll up-vote the good idea, I hadn't thought of it. I think CoreMotion would still need to actively calculate the orientation of the device though, so I'm not sure how much power/cpu this would save, did you do any benchmarks with it? – baris Apr 06 '13 at 21:14
  • I read about this advice long time ago (about 2 years ago, iPhone 4 and iOS 4.x). I made a few tests but not enough to call it benchmarks in a scientific way. My main problem at that time was the initialisation time needed when restarting Core Motion. Possibly this not a problem today. – Kay Apr 08 '13 at 08:10
  • Thanks. It's still a problem, at least for my use case of switching controls dynamically within a game (it takes about a second to get useful updates on my 4s) – baris Apr 08 '13 at 11:19