2012-02-13 60 views
2

我在我的應用程序中使用CMMotionManager,所以我可以得到設備的運動信息。我有兩種方法:CMMotionManager與多任務

- (void)startDeviceMotion { 
    motionManager = [[CMMotionManager alloc] init]; 
    motionManager.showsDeviceMovementDisplay = YES; 
    motionManager.deviceMotionUpdateInterval = 1.0/120.0; 
    [motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical]; 
} 

二是:

- (void)stopDeviceMotion { 
    [motionManager stopDeviceMotionUpdates]; 
    [motionManager release]; 
    motionManager = nil; 
} 

他們推出時,應用程序啓動並當應用程序分別完成。我的問題現在是多任務處理。如果我將問題置於後臺,然後再次將其置於前臺,則會收到一條消息(NSZombie已激活),告訴我[CMMotionManager retain]消息正被髮送到釋放的實例。

我的問題在哪裏?

謝謝!

回答

2

嘗試使用Jonathan's suggestion here。基本上,爲了確保只創建了一個CMMotionManager的實例,請將您的MotionManager置於AppDelegate中,並通過此方法在您想要使用MotionManager的任何位置進行檢索。

-(CMMotionManager *)motionManager 
{ 
    CMMotionManager *motionManager = nil; 
    id appDelegate = [UIApplication sharedApplication].delegate; 
    if ([appDelegate respondsToSelector:@selector(motionManager)]) { 
    motionManager = [appDelegate motionManager]; 
} 
return motionManager; 
} 

讓我知道這是否適合你。

+0

這絕對適合我!我有同樣的問題。今天浪費很多時間爲我修理。非常感謝! :D – Sakares 2012-04-29 15:36:49

+0

非常歡迎。抱歉遲了迴應 :) – Q8i 2012-05-21 10:05:07