I have a class with an ivar:
@interface myCoolClass:NSObject
{
CMAttitude *referenceAttitude;
}
In my implementation have these selectors:
- (void) startTrackingMotion
{
if (motionManager == nil) {
motionManager = [[CMMotionManager alloc] init];
motionManager.accelerometerUpdateInterval = 0.01;
motionManager.deviceMotionUpdateInterval = 0.01;
referenceAttitude = [motionManager.deviceMotion.attitude retain];
}
[motionManager startDeviceMotionUpdates];
if (referenceAttitude == nil) {
CMDeviceMotion *dm = motionManager.deviceMotion;
referenceAttitude = [dm.attitude retain];
}
}
- (void) stopTrackingMotion
{
[motionManager stopDeviceMotionUpdates];
}
I want to take the referenceAttitude when I init the motion manager and use it over the life of the application. Sometimes, I need to track motion and other times I do not.
Here's the app flow:
- call startTrackingMotion since I'm ready for motion
- referenceAttitude stays retained and I use it to track motion
- I call stopTrackingMotion since I'm going to do non motion stuff
- the app do some other stuff
- I call startTrackingMotion again since I'm ready for motion again
At this point, as I step through the code, I step over the "if (motionManager == nil)" loop since it's still there. However, everytime it comes to the "if (referenceAttitude == nil)" loop, the if stateme开发者_JAVA百科nt resolves to true.
Am I retaining it incorrectly? Does calling stopDeviceMotionUpdates nil out my instance?
Thanks.
I dont think you are giving enough time for the motionManager to update itself.
You should get the referenceAttitude after the first tick of deviceMotionUpdateInterval (0.1).
Calling stopDeviceMotionUpdates doesn't nil out your referenceAttitude variable. What version of XCode are you using, and what kind of device are you running it on?
Have you verified that it is set to a valid CMAttitude instance at this point in code?
referenceAttitude = [motionManager.deviceMotion.attitude retain];
If you execute po [dm attitude]
in the debugger within the referenceAttitude == nil
block, what do you get?
精彩评论