心电图期间无法获取用户的平均心率
Posted
技术标签:
【中文标题】心电图期间无法获取用户的平均心率【英文标题】:Cant get user’s average heart rate during the ECG 【发布时间】:2021-09-23 09:42:25 【问题描述】:我按照 Apple Doc 的示例代码成功获取心电图。现在我想获得心电图期间用户的平均心率。我有以下代码:
var ecgSamples = [(Double,Double)] ()
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: HKObjectQueryNoLimit, sortDescriptors: [sortDescriptor]) (query, samples, error) in
guard let samples = samples else
return
let query = HKElectrocardiogramQuery(samples[counter] as! HKElectrocardiogram) (query, result) in
switch result
case .error(let error):
print("error: ", error)
case .measurement(let value):
let sample = (value.quantity(for: .appleWatchSimilarToLeadI)!.doubleValue(for: HKUnit.volt()) , value.timeSinceSampleStart)
print(sample)
ecgSamples.append(sample)
case .done:
print("done")
DispatchQueue.main.async
completion(ecgSamples,samples[counter].startDate)
self.healthStore.execute(query)
self.healthStore.execute(ecgQuery)
我不明白如何选择平均心率?
【问题讨论】:
【参考方案1】:你可以从HKElectrocardiogram
对象获取它:
let electrocardiogram = samples[counter] as! HKElectrocardiogram
let averageHeartRate = electrocardiogram.averageHeartRate?.doubleValue(for: HKUnit.count().unitDivided(by: .minute())) ?? 0
【讨论】:
以上是关于心电图期间无法获取用户的平均心率的主要内容,如果未能解决你的问题,请参考以下文章