开发者

Why does my retained CMAttitude instance become nil after I call stopDeviceMotionUpdates and startDeviceMotionUpdates?

开发者 https://www.devze.com 2023-03-14 20:11 出处:网络
I have a class with an ivar: @interface myCoolClass:NSObject { CMAttitude *referenceAttitude; } In my implementation have these selectors:

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:

  1. call startTrackingMotion since I'm ready for motion
  2. referenceAttitude stays retained and I use it to track motion
  3. I call stopTrackingMotion since I'm going to do non motion stuff
  4. the app do some other stuff
  5. 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消