问题
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);
}
}
回答1:
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.
回答2:
Finally. I found the answer to my own question :)
Apple docs:
Choosing a Motion Event Update Interval
When you request motion data with Core Motion, you specify an update interval. You should choose the largest interval that meets your app’s needs. The larger the interval, the fewer events are delivered to your app, which improves battery life.
Table 4-1 lists some common update frequencies and explains what you can do with data generated at that frequency. Few apps need acceleration events delivered 100 times a second.
Event frequency (Hz) Usage
10–20 Suitable for determining a device’s current orientation vector.
30–60 Suitable for games and other apps that use the accelerometer for real-time user input.
70–100 Suitable for apps that need to detect high-frequency motion. For example, you might use this interval to detect the user hitting the device or shaking it very quickly.
You can set the reporting interval to be as small as 10 milliseconds (ms), which corresponds to a 100 Hz update rate, but most app operate sufficiently with a larger interval.
In a nutshell, as Bogdan said already: 10Hz-100Hz
Source: Choosing a Motion Event Update Interval
来源:https://stackoverflow.com/questions/11797857/safe-update-interval-for-startdevicemotionupdatestoqueuewithhandler