首页 > 解决方案 > HKSampleQuery 产生意外结果

问题描述

.bodyMass我正在尝试从 HealthKit中获取所有记录private let healthType = HKObjectType.quantityType(forIdentifier: .bodyMass)!以及此查询代码:

let mostRecentPredicate = HKQuery.predicateForSamples(
            withStart: Date(timeIntervalSince1970: 1546300800000),
            end: Date(),
            options: .strictEndDate
)

let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)

let query = HKSampleQuery(
           sampleType: healthType,
           predicate: mostRecentPredicate,
           limit: 20, 
           sortDescriptors: [sortDescriptor]
) { (query, results, error) in
    guard let quantityResults = results as? [HKQuantitySample] else {
       completion(nil) 
    }

    // quantityResults will be an empty array, with 0 values.

    // some more irrelevant code for now
}

healthStore.execute(query)

但是,我在 Health 应用程序results的 where 回调中收到了一个数组18235 values,我只有 20 个。此外,当我尝试将结果转换为 时HKQuantitySample,不再有任何值。我使用的框架是错误的,还是其他原因?

更多上下文:我正在使用 WatchKit 应用程序在 WatchKit 扩展中运行此代码。

标签: iosswifthealthkit

解决方案


推荐阅读