【问题标题】:HealthKit Returning Wrong Health Data (Calorie)HealthKit 返回错误的健康数据(卡路里)
【发布时间】:2022-01-15 19:58:47
【问题描述】:

我 6 个月前在 Apple 的开发者论坛上问过这个问题,但没有一个答复。所以我希望你们能做得更好,哈哈

我在 WatchOS 应用中使用 Apple 的 HealthKit API 来检索能量指标,例如活动能量和燃烧的基础能量。我遇到的问题是获取准确的数据。 iPhone 上的 Apple Health App 显示一个值,而通过 HealthKit 返回的数据是另一个值。有时数据是相同的,而其他时候可能会有 500-600 卡路里的增量。这是我的代码示例,展示了我如何使用 HealthKit API 来检索能量数据。

NSDate* StartOfDay = [[NSCalendar currentCalendar] startOfDayForDate:[NSDate now]];
NSDateComponents* Components = [[NSDateComponents alloc] init];
Components.day = 1;
NSDate* EndOfDay = [[NSCalendar currentCalendar] dateByAddingComponents:Components toDate:StartOfDay options:NSCalendarWrapComponents];
HKSampleType* SampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierBasalEnergyBurned];
NSPredicate *Predicate = [HKQuery predicateForSamplesWithStartDate:StartOfDay endDate:EndOfDay options:HKQueryOptionNone];
NSSortDescriptor *SortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:YES];
HKSampleQuery* SampleQuery = [[HKSampleQuery alloc] initWithSampleType:SampleType predicate:Predicate limit:HKObjectQueryNoLimit sortDescriptors:@[SortDescriptor] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error)
{
    if (!error && results)
    {
        int BasalCalBurned = 0;
        for (HKQuantitySample *samples in results)
        {
            BasalCalBurned += [[samples quantity] doubleValueForUnit:[HKUnit largeCalorieUnit]];
        }
    }
}

在 WatchOS 和 iOS 上调用此代码都会导致与上述相同的问题

【问题讨论】:

  • BasalCalBurned 变量似乎应该是双精度,而不是整数。如果你想要一个四舍五入的值,你应该在对样本求和后对它进行 round()。
  • @TyR 有趣的是,我没有意识到我使用了整数数据类型。如果那个循环要运行很多次迭代,那我为什么会离开,这将是完全有道理的。谢谢!我会调查并报告
  • 使用 double 类型确实将值更改为更接近健康应用程序中报告的值,但它仍然始终保持约 50-100 卡路里的减少。不完全确定发生了什么。不过我可以说,燃烧的活动卡路里与健康应用程序报告的内容是 100% 准确的。只是燃烧的基础能量不准确且不准确

标签: ios objective-c apple-watch healthkit


【解决方案1】:

好的,所以我将把它标记为已解决,即使还有一些问题。在@TyR 指出我使用int 数据类型的错误并更正之后,如果我查看Data Sources 部分并选择我的 Apple Watch Data Source 并手动将它收集的所有样本加起来。因此,我确信我的数据是正确的,因为它与 Data Sources 中的值匹配。但是,Health App 概览部分中报告的值与 数据源 部分中的数据以及通过 HealthKit API 报告的数据不同。 Apple 必须执行某种校正算法,或者在跨越昼夜界限时仅获取相关样本。我现在很满意,在修复了int 错误之后,我得到了适当的数据。非常感谢!

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多