我发现从 HealthKit 中请求总和,例如,饮食卡路里将返回一个值,该值会自动减去我自己的应用程序中输入的值。这很好,也是我想要的行为,但这种情况不会一直发生——有时它包括我从我自己的应用程序中创建的条目。这使得在不遍历每个对象的情况下无法明智地使用 HealthKit 统计查询结果。
predicate = [HKQuery predicateForSamplesWithStartDate:[date dateByAddingTimeInterval:-queryLatencyPeriod] endDate:nil options:0];
HKStatisticsQuery *sumQuery;
sumQuery = [[HKStatisticsQuery alloc] initWithQuantityType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed] quantitySamplePredicate:predicate options:HKStatisticsOptionCumulativeSum completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) {
float calories = [[result sumQuantity] doubleValueForUnit:[HKUnit calorieUnit]];
}];
有时这会包括我的应用程序的条目总数,有时则不会。就那么简单。我认为这是 HealthKit 中的一个错误。
最佳答案
统计查询汇总来自多个来源的样本。当来自两个不同来源的累积类型的样本在时间上重叠时,HealthKit 选择一个来源来使用。源的优先级由每个样本类型确定,具体取决于它们在 Health 应用程序中该样本类型的共享数据 View 表的数据源部分中的列出顺序。
我怀疑当您看到自己的应用中的样本没有被计算在内时,实际上是因为来自其他来源的样本与它们重叠。
关于ios - Health Kit Statistics Query - 它是否包括来自自己来源的条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28752850/