CMAltimeter callback never fires

♀尐吖头ヾ 提交于 2019-12-23 22:47:49

问题


Using my 6+ I've been trying to read the relative altitude and pressure using CoreMotion's new CMAltimeter. However the callback is never firing. I have a very similar setup which instead uses the accelerometers, gyros, and magnetometers. They all seem to work fine.

Was wondering if anyone out there has managed to get a reading?

- (void)viewDidLoad {
    [super viewDidLoad];

    if([CMAltimeter isRelativeAltitudeAvailable]){
        CMAltimeter *altimeterManager = [[CMAltimeter alloc]init];
        [altimeterManager startRelativeAltitudeUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAltitudeData *altitudeData, NSError *error) {
            // This never fires.
            NSString *data = [NSString stringWithFormat:@"Altitude: %f %f", altitudeData.relativeAltitude.floatValue, altitudeData.pressure.floatValue];
            NSLog(@"%@", data);
            self.altimeterLabel.text = data;
        }];
        NSLog(@"Started altimeter");
        self.altimeterLabel.text = @"-\n-";
    } else {
        NSLog(@"Altimeter not available");
    }
}

I've tried taking this on a quick walk, but there's only one story of altitude to lose/gain around here.


回答1:


I'm pretty embarrased to answer my own question with such a huge oversight.

In the original post I had the CMAltimiter declared in the scope of viewDidLoad, thus it goes out of scope and is deallocated. I moved it to be an iVar and the callback now fires.

#import "ViewController.h"
@import CoreMotion;

@interface ViewController ()
@property (nonatomic, strong) CMAltimeter *altimeterManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    if([CMAltimeter isRelativeAltitudeAvailable]){
        self.altimeterManager = [[CMAltimeter alloc]init];
        [self.altimeterManager startRelativeAltitudeUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAltitudeData *altitudeData, NSError *error) {
            // This now fires properly
            NSString *data = [NSString stringWithFormat:@"Altitude: %f %f", altitudeData.relativeAltitude.floatValue, altitudeData.pressure.floatValue];
            NSLog(@"%@", data);
            self.altimeterLabel.text = data;
        }];
        NSLog(@"Started altimeter");
        self.altimeterLabel.text = @"-\n-";
    } else {
        NSLog(@"Altimeter not available");
    }
}



回答2:


You need to call [altimeterManager stopRelativeAltitudeUpdates]; for the references to be released to the dispatch queue.



来源:https://stackoverflow.com/questions/26028186/cmaltimeter-callback-never-fires

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