为什么在我调用 stopDeviceMotionUpdates 和 startDeviceMotionUpdates 后,我保留的 CMAttitude 实例变为 nil?

发布于 2024-11-16 16:13:42 字数 1244 浏览 2 评论 0原文

我有一个带有 ivar 的类:

@interface myCoolClass:NSObject 
{
   CMAttitude *referenceAttitude;
}

在我的实现中有这些选择器:

- (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];
}

我想在初始化运动管理器时获取 referenceAttitude 并在应用程序的整个生命周期中使用它。有时,我需要跟踪运动,有时则不需要。

这是应用程序流程:

  1. 调用startTrackingMotion,因为我已准备好运动
  2. 参考Attitude保持保留,我用它来跟踪运动
  3. 我调用stopTrackingMotion,因为我要做非运动的东西,
  4. 应用程序做一些其他东西
  5. ,我再次调用startTrackingMotion,因为我'我准备好再次运动

此时,当我单步执行代码时,我会跳过“if (motionManager == nil)”循环,因为它仍然存在。然而,每次进入“if (referenceAttitude == nil)”循环时,if 语句都会解析为 true。

我是否错误地保留了它?调用 stopDeviceMotionUpdates 会使我的实例为零吗?

谢谢。

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 statement resolves to true.

Am I retaining it incorrectly? Does calling stopDeviceMotionUpdates nil out my instance?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饮惑 2024-11-23 16:13:42

我认为您没有给 MotionManager 足够的时间来更新自身。

您应该在 deviceMotionUpdateInterval (0.1) 的第一个刻度之后获取 referenceAttitude。

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).

离线来电— 2024-11-23 16:13:42

调用 stopDeviceMotionUpdates 不会清空您的 referenceAttitude 变量。您使用什么版本的 XCode?您在什么类型的设备上运行它?

您是否已验证此时在代码中是否已将其设置为有效的 CMAttitude 实例?

referenceAttitude = [motionManager.deviceMotion.attitude retain];

如果您在调试器中的 referenceAttitude == nil 块中执行 po [dm Attitude],您会得到什么?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文