我正在尝试使用.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)
但是,我在results
的回调函数中收到一个18235 values
数组,其中在Health应用程序中,我只有20。此外,当我尝试将结果转换为{{1} },没有任何值了。我使用的框架是否错误,还是有其他原因?
更多上下文:我正在使用WatchKit应用在WatchKit扩展中运行此代码。