gpt4 book ai didi

ios - 无法从 HealthKit 获取合理格式的 HRV 读数

转载 作者:行者123 更新时间:2023-11-28 15:12:50 50 4
gpt4 key购买 nike

我需要能够从 HealthKit 读取所有 HRV 读数,并根据它们的创建日期对它们的值进行排序。

我可以使用 SampleQuery 从 HealthKit 读取特定时间间隔内的所有读数,如下所示:

func getHRVSampleQuery() {
let HRVType = HKQuantityType.quantityType(forIdentifier: .heartRateVariabilitySDNN)

let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false)

let startDate = Date() - 7 * 24 * 60 * 60 // start date is a week from now
// Set the Predicates & Interval
let predicate: NSPredicate? = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: HKQueryOptions.strictEndDate)

let sampleQuery = HKSampleQuery(sampleType: HRVType!, predicate: predicate, limit: 30, sortDescriptors: [sortDescriptor]) { sampleQuery, results, error in
if(error == nil) {
for result in results! {
print("Startdate")
print(result.startDate)
print(result.sampleType)
print(result)
// print(result.metadata)
}
}
}
healthStore.execute(sampleQuery)
}

打印 90.4091 ms AC994386-6981-496A-9C0C-5F6839664302 "Apple Watch van Bas"(4.0), "Watch3,4"(4.0)"Apple Watch"(2017-11-10 15:58 :21 +0000 - 2017-11-10 16:03:32 +0000)

完美!我需要的所有值:-)

但是,似乎没有办法得到90.4091ms...

所以,我目前正在通过查询 HKStatisticsCollectionQuery 来解决这个问题。

func getHRV() -> Void {
// Define the Step Quantity Type
let HRVType = HKQuantityType.quantityType(forIdentifier: .heartRateVariabilitySDNN)

// Get the start of the day
let date = Date() - 31 * 24 * 60 * 60
let cal = Calendar(identifier: Calendar.Identifier.gregorian)
let newDate = cal.startOfDay(for: date)

// Set the Predicates & Interval
let startDate = Date() - 7 * 24 * 60 * 60 // start date is a week
var predicate: NSPredicate? = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: HKQueryOptions.strictEndDate)

// Define interval
var interval = DateComponents()
interval.second = 7

// Perform the Query
let query = HKStatisticsCollectionQuery(quantityType: HRVType!, quantitySamplePredicate: predicate, options: .separateBySource, anchorDate: startDate, intervalComponents:interval)

query.initialResultsHandler = { query, results, error in

if error != nil {
print("Cannot read HRV from HealthKit. Either the user hasn't given permission or permissions are not set.")
// Something went Wrong
return
}

print(results?.sources())

if let myResults = results {
print("\(results! as HKStatisticsCollection)")
print("results")
print(results?.sources())

let startDate = Date() - 7 * 24 * 60 * 60
print("startDate")
print(startDate)
print("End date")
print(Date() as Date)
print(myResults)
myResults.enumerateStatistics(from: startDate, to: Date() as Date) { statistics, stop in
if let quantity = statistics.averageQuantity() {
print(statistics)
}
} //end block
}

healthStore.execute(query)
}

这里的问题是结果包含正确的 HRV 值,但在枚举结果时,我什么也没得到。

我在那个街区做错了什么吗?

最佳答案

您可以通过如下调整代码来获取HRV值(以ms为单位):

let sampleQuery = HKSampleQuery(sampleType: HRVType!, predicate: predicate, limit: 30, sortDescriptors: [sortDescriptor]) { sampleQuery, results, error  in
if(error == nil) {
for result in results! {
print("Startdate")
print(result.startDate)
print(result.sampleType)
print(result.quantity.doubleValue(for: HKUnit.secondUnit(with: .milli)))
print(result)
// print(result.metadata)
}
}
}

关于ios - 无法从 HealthKit 获取合理格式的 HRV 读数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47355242/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com