大佬教程收集整理的这篇文章主要介绍了ios – 在控制器内调用startDeviceMotionUpdatesToQueue时得到OSSpinLockLock,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@property (strong,nonatomiC) CMMotionManager *MManager;
在它的吸气器中我做懒惰的实例化.当控制器的视图加载时,我调用此方法
- (void)reloadAccelerometer { NSLog(@"Away we go"); self.MManager.deviceMotionupdateInterval = 10.0/60.0; [self.MManager startDeviceMotionupdatesToQueue:self.queue withHandler:^(CMDeviceMotion *motion,NSError *error) { NSLog(@"Y values is: %f",motion.userAcceleration.y); }]; }
我在NSLog中看到“Away we go”,然后应用程序立即崩溃,我得到了这个线程日志
libsystem_platform.dylib`spin_lock$VARIANT$mp: 0x39a87814: movs r1,#1 libsystem_platform.dylib`OSSpinLockLock$VARIANT$mp + 2: 0x39a87816: ldrex r2,[r0] 0x39a8781a: cmp r2,#0 0x39a8781c: it ne 0x39a8781e: bne.w 0x39a893ec ; _OSSpinLockLockSlow$shim 0x39a87822: strex r2,r1,[r0] 0x39a87826: cmp r2,#0 0x39a87828: bne 0x39a87816 ; OSSpinLockLock$VARIANT$mp + 2 0x39a8782a: dmb ish 0x39a8782e: bx lr
我的错是什么?我把reloadAccelerometer放在错误的地方吗?
NSOperationQueue是罪魁祸首.您的代码没有显示您是如何创建NSOperationQueue的,但我认为它是这样的:
NSOperationQueue *aQueue = [[NSOperationQueue alloc] init]; // Do NOT do this [self.MManager startDeviceMotionupdatesToQueue:aQueue withHandler:^(CMDeviceMotion *motion,NSError *error) { NSLog(@"Y values is: %f",motion.userAcceleration.y); }];
事实证明,这不是使用NSOperationQueue的方法.那个aQueue对象是崩溃的原因.
要正确设置操作队列并避免崩溃,您应该将CMMotionManager移动到其他线程.然后告诉NSOperationQueue使用currentQueue,而不是mainQueue. Apple建议它不在mainQueue上运行,但是如果您的应用程序当前正在主队列中运行,那么我不会看到currentQueue有何不同.我尝试使用GCD将下面的代码移动到不同的队列,但是没有调用任何代码.
以下是您的最终代码应如下所示:
// check if Motion / LOCATIOn services are available if (motionManager.deviceMotionAvailable == YES && motionManager.accelerometerAvailable == YES) { NSLog(@"Away we go"); self.MManager.deviceMotionupdateInterval = 10.0/60.0; [self.MManager startDeviceMotionupdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion,motion.userAcceleration.y); }]; } else { // Motion / Accelerometer services unavailable }
我还应该注意,你创建CMMotionManager属性(据我所知)是正确的(强,非原子).
以上是大佬教程为你收集整理的ios – 在控制器内调用startDeviceMotionUpdatesToQueue时得到OSSpinLockLock全部内容,希望文章能够帮你解决ios – 在控制器内调用startDeviceMotionUpdatesToQueue时得到OSSpinLockLock所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。