如何获得 ECG 电压测量值 HKElectrocardiogram.VoltageMeasurement iOS 14
How to get ECG Voltage Measurements HKElectrocardiogram.VoltageMeasurement iOS 14
我正在尝试使用 iOS 14 中的新 API 从 Apple HealthKit 获取心电图的单个电压测量值。
我已经成功使用了:
let ecgQuery = HKSampleQuery(sampleType: HKObjectType.electrocardiogramType(), predicate: samplePredicate, limit: 0, sortDescriptors: [sortDescriptor]){ (query, results, error) in
这让我得到一个 HKElectrocardiogram 对象。从这里我可以看到平均心率、心电图分类等...
我现在认为我需要像这样将该对象传递给 HKElectrocardiogramQuery:
let ecgSample = HKElectrocardiogramQuery(ecg) { (query, result) in
但我找不到任何方法从结果数据处理程序中提取数据。如果我在结果上打印,它会执行很多次,但我还是无法提取数据。结果的类型为 HKElectrocardiogramQuery.Result
Apple 开发者网站上的文档非常粗略,提供的示例为零。尽管在 WWDC 2020 的 Apple 的 HealthKit 新功能演讲中提到了该功能。非常感谢任何帮助。
干杯
根据可用的documentation,您必须切换结果才能获得测量值
let query = HKElectrocardiogramQuery(ecg) { (query, result) in
switch result {
case .error(let error):
print("error: ", error)
case .measurement(let value):
print("value: ", value)
case .done:
print("done")
}
}
store.execute(query)
如果有人想要这里的完整示例代码,它是:
if #available(iOS 14.0, *) {
let predicate = HKQuery.predicateForSamples(withStart: Date.distantPast,end: Date.distantFuture,options: .strictEndDate)
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
let ecgQuery = HKSampleQuery(sampleType: HKObjectType.electrocardiogramType(), predicate: predicate, limit: 0, sortDescriptors: [sortDescriptor]){ (query, samples, error) in
guard let samples = samples,
let mostRecentSample = samples.first as? HKElectrocardiogram else {
return
}
print(mostRecentSample)
var ecgSamples = [(Double,Double)] ()
let query = HKElectrocardiogramQuery(mostRecentSample) { (query, result) in
switch result {
case .error(let error):
print("error: ", error)
case .measurement(let value):
print("value: ", value)
let sample = (value.quantity(for: .appleWatchSimilarToLeadI)!.doubleValue(for: HKUnit.volt()) , value.timeSinceSampleStart)
ecgSamples.append(sample)
case .done:
print("done")
}
}
self.healthMonitor.healthStore.execute(query)
}
healthMonitor.healthStore.execute(ecgQuery)
} else {
// Fallback on earlier versions
}
我正在尝试使用 iOS 14 中的新 API 从 Apple HealthKit 获取心电图的单个电压测量值。
我已经成功使用了:
let ecgQuery = HKSampleQuery(sampleType: HKObjectType.electrocardiogramType(), predicate: samplePredicate, limit: 0, sortDescriptors: [sortDescriptor]){ (query, results, error) in
这让我得到一个 HKElectrocardiogram 对象。从这里我可以看到平均心率、心电图分类等...
我现在认为我需要像这样将该对象传递给 HKElectrocardiogramQuery:
let ecgSample = HKElectrocardiogramQuery(ecg) { (query, result) in
但我找不到任何方法从结果数据处理程序中提取数据。如果我在结果上打印,它会执行很多次,但我还是无法提取数据。结果的类型为 HKElectrocardiogramQuery.Result
Apple 开发者网站上的文档非常粗略,提供的示例为零。尽管在 WWDC 2020 的 Apple 的 HealthKit 新功能演讲中提到了该功能。非常感谢任何帮助。
干杯
根据可用的documentation,您必须切换结果才能获得测量值
let query = HKElectrocardiogramQuery(ecg) { (query, result) in
switch result {
case .error(let error):
print("error: ", error)
case .measurement(let value):
print("value: ", value)
case .done:
print("done")
}
}
store.execute(query)
如果有人想要这里的完整示例代码,它是:
if #available(iOS 14.0, *) {
let predicate = HKQuery.predicateForSamples(withStart: Date.distantPast,end: Date.distantFuture,options: .strictEndDate)
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
let ecgQuery = HKSampleQuery(sampleType: HKObjectType.electrocardiogramType(), predicate: predicate, limit: 0, sortDescriptors: [sortDescriptor]){ (query, samples, error) in
guard let samples = samples,
let mostRecentSample = samples.first as? HKElectrocardiogram else {
return
}
print(mostRecentSample)
var ecgSamples = [(Double,Double)] ()
let query = HKElectrocardiogramQuery(mostRecentSample) { (query, result) in
switch result {
case .error(let error):
print("error: ", error)
case .measurement(let value):
print("value: ", value)
let sample = (value.quantity(for: .appleWatchSimilarToLeadI)!.doubleValue(for: HKUnit.volt()) , value.timeSinceSampleStart)
ecgSamples.append(sample)
case .done:
print("done")
}
}
self.healthMonitor.healthStore.execute(query)
}
healthMonitor.healthStore.execute(ecgQuery)
} else {
// Fallback on earlier versions
}