Search code examples
iphoneobjective-cioscore-motioncmmotionmanager

Safe update interval for startDeviceMotionUpdatesToQueue:withHandler:?


EDIT: added a global and now it's working. But I still have my doubts.. Please read on :)

I want to get the acceleration exercised on the Y-axis whenever I need to and use it in different parts of my code. In this example I'm using it inside a while-loop for testing purposes..

My code is working but Am I using the the UpdateToQueue... method correctly or is this kind of an "unorthodox" way of achieving what I want?

I've set the Update Interval at 30 ms, do you think this is a "safe" update interval ? I was told that I should be careful when choosing one because current or later hardware/iOS updates might not be able to keep up with such an interval is this true?

double myAcceleration; // a global..

-(void) play // my "main" method..
{
    CMMotionManager *motionManager = [[CMMotionManager alloc] init];
    motionManager.deviceMotionUpdateInterval = 0.03; // update every 30ms
    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]  
                                       withHandler:^(CMDeviceMotion *motion, NSError *error) 
                                       {
                                           myAcceleration = motion.userAcceleration.y;
                                       }
    ];

    while(!self.stopButtonPressed)
    {
        NSLog(@"Y-Axis acceleration is %f", myAcceleration);
    }
}

Solution

  • It is ok to use in such way.

    And about time interval: current maximum update limit is 100Hz( 100 times per second, 0.01 second for one accelerometer update), and minimum is 10Hz( AFAIK ). if you have set time interval in such ranges it will surely be supported in current and next releases of apple gadgets. But you can't rely on default time interval and pray that it will be the same everywhere, cause apple may change min and max( ex. some new IC chip would support 500Hz refresh rate ) ranges, and so default would change too.