ios - 使用 Swift 从 HealthKit 获取过去 30 天的 bodyMass 数据
问题描述
我现在正在学习 Swift 和 HealthKit 并且有点卡在检索数据上。我已经成功查询dietaryCaloriesConsumed
了最近 30 天的我的打印到控制台的信息。
当我尝试对体重数据做同样的事情时,我没有得到任何输出。尽管我可以通过倒数日期并仅使用第一个样本来获取特定日期的体重数据。
我现在的代码:
func getWeightData2(forDay days: Int, completion: @escaping ((_ weight: Double?, _ date: Date?) -> Void)) {
// Getting quantityType as stepCount
guard let weight = HKObjectType.quantityType(forIdentifier: .bodyMass) else {
print("*** Unable to create a step count type ***")
return
}
let now = Date()
let startDate = Calendar.current.date(byAdding: DateComponents(day: -days), to: now)!
var interval = DateComponents()
interval.day = 1
var anchorComponents = Calendar.current.dateComponents([.day, .month, .year], from: now)
anchorComponents.hour = 0
let anchorDate = Calendar.current.date(from: anchorComponents)!
let query = HKStatisticsCollectionQuery(quantityType: weight,
quantitySamplePredicate: nil,
options: [.mostRecent],
anchorDate: anchorDate,
intervalComponents: interval)
query.initialResultsHandler = { _, results, error in
guard let results = results else {
print("ERROR")
return
}
results.enumerateStatistics(from: startDate, to: now) { statistics, _ in
if let sum = statistics.sumQuantity() {
let weight = Int(sum.doubleValue(for: HKUnit.gramUnit(with: .kilo)).roundToDecimal(2))
print("Weight Tracked: \(weight), on date: \(statistics.endDate)")
// completion(calories, statistics.endDate)
return
}
}
completion(nil, nil)
}
healthStore.execute(query)
}
有了这个,我没有得到任何输出。稍微改变一下,dietaryCaloriesConsumed
我就得到了 30 天的卡路里,每天总结(带选项cumulativeSum
)
真正起作用的是检索特定日期的数据,如下所示:
func getWeightData(forDay days: Int) {
let quantityType: HKQuantityType = HKQuantityType.quantityType(forIdentifier: .bodyMass)!
// fetch last 30 days
let startDate = Date.init(timeIntervalSinceNow: TimeInterval(-days*24*60*60))
let endDate = Date()
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate)
let sampleQuery = HKSampleQuery.init(sampleType: quantityType, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, samples, error) in
DispatchQueue.main.async {
guard let samples = samples else { return }
let mostRecentSample = samples.first as? HKQuantitySample
let bodyMass = mostRecentSample?.quantity.doubleValue(for: HKUnit.gramUnit(with: .kilo)).roundToDecimal(2)
print("Weight tracked: \(bodyMass) on date: \(endDate)")
}
}
healthStore.execute(sampleQuery)
}
从我的 ViewController 我调用这些函数是这样的:
profileStore.getTotalCalories(forDay: 30) {_,_ in
}
和
profileStore.getWeightData(forDay: 30)
这行得通,但我相信还有很大的批准空间。
任何想法我的第一个代码做错了什么?获得 n 天价值dietaryCaloriesConsumed
或bodyMass
进行进一步处理的最佳方法是什么?
非常感谢 :)
解决方案
我刚刚解决了我自己的问题:
func getWeightData(forDay days: Int, completion: @escaping ((_ weight: Double?, _ date: Date?) -> Void)) {
// Getting quantityType as stepCount
guard let bodyMassType = HKObjectType.quantityType(forIdentifier: .bodyMass) else {
print("*** Unable to create a bodyMass type ***")
return
}
let now = Date()
let startDate = Calendar.current.date(byAdding: DateComponents(day: -days), to: now)!
var interval = DateComponents()
interval.day = 1
var anchorComponents = Calendar.current.dateComponents([.day, .month, .year], from: now)
anchorComponents.hour = 0
let anchorDate = Calendar.current.date(from: anchorComponents)!
// Note to myself:: StatisticsQuery!! Nicht Collection! Option .mostRecent. Achtung, unten auch setzen!!
let query = HKStatisticsCollectionQuery(quantityType: bodyMassType,
quantitySamplePredicate: nil,
options: [.mostRecent],
anchorDate: anchorDate,
intervalComponents: interval)
query.initialResultsHandler = { _, results, error in
guard let results = results else {
print("ERROR")
return
}
results.enumerateStatistics(from: startDate, to: now) { statistics, _ in
// hier wieder .mostRecent!
if let sum = statistics.mostRecentQuantity() {
let bodyMassValue = sum.doubleValue(for: HKUnit.gramUnit(with: .kilo)).roundToDecimal(2)
completion(bodyMassValue, statistics.startDate)
return
}
}
}
healthStore.execute(query)
}
这会加载最后n
几天的数据并返回 bodyMass 以及日期
推荐阅读
- gulp - Gulp 4手表不工作,只运行一次
- regex - 使用正则表达式提取数据
- react-native - 从一个屏幕导航到另一个屏幕在使用 wix react native navigation V2 时不起作用
- graphql - 运行 apollo-server 时获取节点“错误:找不到模块'core-js/proposals/array-flat-and-flat-map'”
- python - 使用 Panda 通过匹配日期来合并 .csv 文件
- sql - 从表列中的 SQL Server 日期值获取月份名称
- c++ - 宏定义中的空 # 是什么意思?
- python - 如何将 .cer 文件(我拥有的数字签名证书)添加到 pdf?
- c++ - 为 postgresql 编译 libpqxx C++ 库时找不到 libpq-fe.h
- machine-learning - 我可以同时在测试和训练数据上使用 CountVectorizer 还是需要将其拆分?