ios - Can you retrieve different HKQuantity sources together?
问题描述
As a disclaimer, I am new to swift and am mainly relying on other coding experience. I am working on an app that predicts the blood glucose drop of a Type 1 Diabetic after exercise. This requires the input of multiple different Healthkit quantities, including active calories, insulin, and blood glucose, but I have had trouble fetching these quantities to do math with them in the same place. I am working on the foundation of SpeedySloth, Apple's example workout app, and this is how they retrieve Healthkit data:
case HKQuantityType.quantityType(forIdentifier: .heartRate):
/// - Tag: SetLabel
let heartRateUnit = HKUnit.count().unitDivided(by: HKUnit.minute())
let value = statistics.mostRecentQuantity()?.doubleValue(for: heartRateUnit)
let roundedValue = Double( round( 1 * value! ) / 1 )
label.setText("\(roundedValue) BPM")
case HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned):
let energyUnit = HKUnit.kilocalorie()
let value = statistics.sumQuantity()?.doubleValue(for: energyUnit)
let roundedValue = Double( round( 1 * value! ) / 1 )
label.setText("\(roundedValue) cal\n")
As you can see, a new case is created every time a quantity is recalled, and each one is called separately. However, is there any way to call them together, or multiple at once, so I can work with the quantities? I tried other methods, including a Predicate within one of the cases, but that did not seem to work. Any suggestions would be appreciated.
Since you cannot query for multiple quantities within the same query, is it possible to nest a predicate query within the quantity recall so that I could work with both variables? Here is what I tried to put a predicate query within the active energy case:
let energyUnit = HKUnit.kilocalorie()
let value = statistics.sumQuantity()?.doubleValue(for: energyUnit)
let roundedValue = Double( round( 1 * value! ) / 1 )
let quantityType = HKObjectType.quantityType(forIdentifier:(HKQuantityTypeIdentifier.bloodGlucose))
let mostRecentPredicate = HKQuery.predicateForSamples(withStart: Date.distantPast,
end: Date(),
options: .strictEndDate)
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate,
ascending: false)
let limit = 1
let sampleQuery = HKSampleQuery(sampleType: quantityType!,
predicate: mostRecentPredicate,
limit: limit,
sortDescriptors: [sortDescriptor]) { (query, samples, error) in
let sample = samples?.first as? HKQuantitySample
let BG = sample?.quantity
let ESF = 0.125
let TotBGChg = -1 * ESF * roundedValue
let roundedTBGC = round(TotBGChg)
label.setText("\(roundedValue) cal\n\(String(describing: BG)) mg/dL")
}
return
解决方案
推荐阅读
- python-3.x - json dict to excel - python
- java - 如何使用 Geckodriver 保留 Firefox 配置文件的缓存?
- asp.net - 更新面板不工作
- sendgrid - 使用 sendgrid 发送电子邮件失败并出现 500 内部错误
- file-upload - SAP UI5 使用 OData 上传图像文件
- sql - 将 sql 转换为 dsl elasticsearch 查询
- ios - 当应用程序在 ios 中被杀死时,如何打开应用程序并显示推送通知
- django - url文件中的python3注释出错
- amazon-dynamodb - 在特定时间更新 DynamoDB 条目
- c++ - 如何在没有 OPENPROCESS 的情况下向句柄授予 RPM/WPM 权限?