- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要能够从 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/
我不知道如何获得“添加到健康的日期”?我无法从 HKSample 找到有关“添加到健康的日期”的一些信息。 任何人都可以帮助我吗? HKSample : HKObject open var s
昨天苹果宣布了他们的 health records API . documentation for it指出: ...clinical records are read-only, so you ca
我在我的 iOS Swift 应用程序中添加 HealthKit 代码,但出现错误: /* Ask for permission to access the health store */ overr
我的 watch 应用程序能够轻松地从 HealthKit 的 HKAnchoredObjectQuery 中的新 updateHandler 读取新的心率数据。 问题是,手机应用程序观察到的健康商店
尝试使用 HealthKit API 授权时,我们在某些设备上遇到错误。对 [HKHealthStore requestAuthorizationToShareTypes:::] 的调用始终失败并出现
我正在尝试做一个非常基本的事情:设置一个HKObserverQuery,以便我可以知道何时更改了各个数据点(我已确保用户已授权该应用程序使用所涉及的数据点。) ,我可以使查询在每次启动该应用程序时触发
我正在尝试制作一个基本脚本,从 iOS 上的 HealthKit 中提取我的每日步数,但是当触发页面时 - 什么也没有发生。它应该请求读取 HealthKit 数据的权限,但我不知道哪里出了问题。 这
我有一个 Watch-App,几周前创建了一个分发配置文件,有一个启用 HealthKit 的选项。 现在发现我不需要Watch App中的HealthKit。(因此没有在 WatchKit Exte
我正在使用 HealthKit 在我的应用程序中显示步骤,但它非常延迟。我正在使用其他实时显示步骤的应用程序,但感觉 HealtKit 有几分钟的延迟? 这是接收步骤的代码: func querySt
大家好,有人知道如何从 HealthKit 获取锻炼数据吗?我在本教程中看到了http://www.raywenderlich.com/89733/healthkit-tutorial-with-sw
在我的应用程序中,我想每天对两个数量的 ActiveEnergyBurned 和 BasalEnergyBurned 求和并显示出来。是否可以使用单个 HKQuery 来执行此操作?还是我必须单独获取
我已经在委托(delegate)的 didFinishLaunching 方法中注册了四种类型的 HealthKit 数据的后台传送。数据类型为步数、 sleep 、锻炼和energyConsumed
我正在尝试从用户的设备获取每天的平均心率。我有下面的代码,在调用时打印“avgHeart”,但不执行任何其他操作。 我正在自己的手机上进行测试,手机上肯定有心率数据,并且已被授权读取心率数据。 fun
我一直在寻找这个问题的答案,但找不到任何好的线索。我制作了一个药物应用程序,希望能够将药物数据推送到 HealthKit 应用程序。这个功能现在可用吗? 最佳答案 HealthKit 目前不支持药物。
我有一个应用程序可以从 Strava、Runkeeper 和其他几个健康应用程序读取数据。现在我计划将 HealthKit 集成到我的应用程序中并从那里读取数据。 我担心的是数据的重复性。也就是说,S
我在尝试集成 HealthKit 时收到“未找到匹配的配置文件”错误。我已经添加了 HealthKit 权利。我已经重新生成证书并尝试过,但仍然出现错误。项目和目标都选择了相同的证书。我在 Xcode
我正在尝试将一些数据保存到 HealthKit。为每个项目发送一个 UUID。它是一个转换成字符串的 NSUUID。 hk_acceptsMetadataValue:]: unrecognized s
我一直在尝试在手机上查询 HealthKit 的步数。这是我尝试过的: let endDate = NSDate() let startDate = NSCalendar.curren
用户的健康相关数据属于隐私事项,需要非常安全,即需要将其存储在安全的地方。例如,当我观看苹果关于指纹解锁的官方视频时,他们告诉我指纹数据存储在无法破解的地方。因此,出于好奇,我想到了一个问题:这些数据
创建HKObject后,有什么方法可以修改其中的元数据吗?我知道我们可以在创建期间修改元数据(NSdictionary),但如果我需要在之后添加额外的数据怎么办? 最佳答案 存储在 HealthKit
我是一名优秀的程序员,十分优秀!