Why is this CMDeviceMotionHandler never called by CoreMotion?

社会主义新天地 提交于 2019-12-14 03:56:00

问题


I've included the CoreMotion framework in my project and imported the CoreMotion framework in the header of my view controller:

#import <CoreMotion/CoreMotion.h>

Then in -viewDidLoad I have this simple test code which I run on an iPhone 4 with iOS 4.3:

- (void)viewDidLoad {
    [super viewDidLoad];

    CMMotionManager *motionManager = [[CMMotionManager alloc] init];
    [motionManager setDeviceMotionUpdateInterval:0.1];

    CMDeviceMotionHandler  motionHandler = ^(CMDeviceMotion *motion, NSError *error) {

        NSLog(@"foo");

    };

    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:motionHandler];

    NSLog(@"merde!");
}

When I run the project on the device and move it around, I never get a "foo" log. Also, I tried setting a breakpoint in the motion handler block and it never halts there. But I do get the "merde!" log, so this code is definitely executed.

Why is core motion not calling my handler? My iPhone 4 is functional. Accelerometers and gyro work perfectly in other apps. There is no hardware bug.


回答1:


I had very similar code running successfully, same applies to the available samples in the Event Handling Guide for iOS (there is only an appropriate one for gyro data). But there is one major difference:

All implementations hold their own reference to the opereation queue with operationQueue = [[NSOperationQueue currentQueue] retain]; or build their own. So hopefully this helps to get more than 'merde' in your logs ;-)



来源:https://stackoverflow.com/questions/10832650/why-is-this-cmdevicemotionhandler-never-called-by-coremotion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!