Question

Is it possible to request the values of the Accelerometer/Gyroscope without using continuous delegation (like using UIAccelerometerDelegate or something like CMMotionManager startGyroUpdatesToQueue)? I mean, I want to request the values for x, y and z, just once and this as resource-efficient as possible.

thank you.

Was it helpful?

Solution

This should work.

CMMotionManager* motionManager = [[CMMotionManager alloc] init];
[motionManager startAccelerometerUpdates];
CMAccelerometerData* data = [motionManager accelerometerData];
while ( data.acceleration.x == 0 )
{
    data = [motionManager accelerometerData];
}
NSLog(@"x = %f, y = %f, z = %f.", data.acceleration.x, data.acceleration.y, data.acceleration.z);

This is just an example of what you could do to make things work. This is a pretty bad idea in my opinion, but it does make things work if you want to get it done.

The code is pretty simple and straightforward, you tell a CMMotionManager to start retrieving data from accelerometer or gyroscope, and then you request the data from it. Here why it's bad, updates have intervals, so for the first few milliseconds, the data retrieved from CMMotionManager is zero-valued. That's what the while loop is for. This type of things should be offloaded to another thread so the UI won't be blocked, but it's even more resource-consuming than UIAccelerometerDelegate and the blocking won't be more than half a second.

OTHER TIPS

If you need data only 3 times per hour you can just stop delegation using

accelerometer.delegate = nil;

This should prevent the loss of resources (at least enough to tell system that your app do not needs accelerometer data at the moment)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top