尝试从 HealthKit Store 转换心率时出现异常

Posted

技术标签:

【中文标题】尝试从 HealthKit Store 转换心率时出现异常【英文标题】:Exception when trying to convert HeartRate from HealthKitStore 【发布时间】:2015-06-16 12:40:22 【问题描述】:

我正在开发我的第一个 iPhone 应用程序:一个简单的应用程序,以一种很好的方式显示 HealthKit 的心率结果。我的第一步是将结果显示为原始文本。但不幸的是,我在下一行遇到了一个异常,告诉我:“线程 1 信号 SIGABRT”。有人知道我做错了什么并提示我一个方向吗?

double usersBeatsPerMinute = [quantity doubleValueForUnit:[HKUnit countUnit]];

其余代码如下所示:

- (void)viewDidLoad 
[super viewDidLoad];
// Do any additional setup after loading the view.
// Set up an HKHealthStore, asking the user for read/write permissions. The profile view controller is the
// first view controller that's shown to the user, so we'll ask for all of the desired HealthKit permissions now.
// In your own app, you should consider requesting permissions the first time a user wants to interact with
// HealthKit data.
if ([HKHealthStore isHealthDataAvailable]) 
    NSSet *writeDataTypes = [self dataTypesToWrite];
    NSSet *readDataTypes = [self dataTypesToRead];

    [self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) 
        if (!success) 
            NSLog(@"You didn't allow HealthKit to access these read/write data types. In your app, try to handle this error gracefully when a user decides not to provide access. The error was: %@. If you're using a simulator, try it on a device.", error);

            return;
        

    ];


HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];

// Since we are interested in retrieving the user's latest sample
// we sort the samples in descending order by end date
// and set the limit to 1
// We are not filtering the data, and so the predicate is set to nil.
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];

// construct the query & since we are not filtering the data the predicate is set to nil
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:weightType predicate:nil limit:1 sortDescriptors:@[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) 

    // if there is a data point, dispatch to the main queue
    if (results) 
        dispatch_async(dispatch_get_main_queue(), ^
            HKQuantitySample *quantitySample = results.firstObject;

            // pull out the quantity from the sample
            HKQuantity *quantity = quantitySample.quantity;

            double usersBeatsPerMinute = [quantity doubleValueForUnit:[HKUnit countUnit]];
            _HeartRateResults.text = [NSString stringWithFormat:@"%@ lbs", [NSNumberFormatter localizedStringFromNumber:@(usersBeatsPerMinute) numberStyle:NSNumberFormatterNoStyle]];
        );
    
];

// do not forget to execute the query after its constructed
[_healthStore executeQuery:query];

【问题讨论】:

先检查数量是否兼容。 [quantity isCompatibleWithUnit:[HKUnit countUnit]]; 试一试。感谢您的快速响应 将代码更改为这个,现在它没有崩溃,但它没有显示hearRate ether:bool compatibilityQuantityAndCountUnit = [quantity isCompatibleWithUnit:[HKUnit countUnit]]; if (compatibilityQuantityAndCountUnit) double usersBeatsPerMinute = [quantity doubleValueForUnit:[HKUnit countUnit]]; _HeartRateResults.text = [NSString stringWithFormat:@"%@ lbs", [NSNumberFormatter localizedStringFromNumber:@(usersBeatsPerMinute) numberStyle:NSNumberFormatterNoStyle]]; 所以您的数量样本有问题。 【参考方案1】:

documentation 中有一条评论(“这些样本使用计数/时间单位”)我不太明白,所以我做了一些搜索并尝试了一下,并且能够得到我手动输入的值使用这个进入健康应用程序:

double rate = [mostRecentQuantity doubleValueForUnit:[[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]]];

我以前没见过unitDividedByUnit。这是the article我从这里拉出来的。

【讨论】:

这行得通。不知道没有你的帖子我怎么会想出来。非常感谢!

以上是关于尝试从 HealthKit Store 转换心率时出现异常的主要内容,如果未能解决你的问题,请参考以下文章

从 HealthKit 检索心率数据时遇到问题

从 healthKit 中提取心率

从 healthKit 中提取心率

Swift HealthKit 触发心率样本

Apple Healthkit 的心率

Apple Healthkit 的心率