在 Series 5 Watch 和 Watch OS6 上使用“HKAnchoredObjectQuery”返回心率值时出现问题
Problems returning heart rate values using `HKAnchoredObjectQuery` on Series 5 Watch and Watch OS6
我正在使用 HKAnchoredObjectQuery
查询心率,因为我不想为此应用程序使用 WorkoutBuilder API。此查询始终以正常采样率(每秒等)在锻炼期间返回心率,但我注意到在 5 系列手表 OS 6.1 上进行测试时,我只看到非常零星的样本,例如一小时内测得 5 次心率。 HKAnchoredObjectQuery
的文档中没有任何内容表明它已被弃用。知道为什么这种心率收集方法不再有效了吗?
func startHeartRateQuery(from startDate: Date, updateHandler: @escaping ([HKQuantitySample]) -> Void) {
let typeIdentifier = HKQuantityTypeIdentifier.heartRate
startQuery(ofType: typeIdentifier, from: startDate) { _, samples, _, _, error in
guard let quantitySamples = samples as? [HKQuantitySample] else {
print("Heart rate query failed with error: \(String(describing: error))")
return
}
updateHandler(quantitySamples)
}
}
//Generic helper function
private func startQuery(ofType type: HKQuantityTypeIdentifier, from startDate: Date, handler: @escaping
(HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void) {
let datePredicate = HKQuery.predicateForSamples(withStart: startDate, end: nil, options: .strictStartDate)
let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
let queryPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:[datePredicate, devicePredicate])
let quantityType = HKObjectType.quantityType(forIdentifier: type)!
let query = HKAnchoredObjectQuery(type: quantityType, predicate: queryPredicate, anchor: nil,
limit: HKObjectQueryNoLimit, resultsHandler: handler)
query.updateHandler = handler
healthStore.execute(query)
activeDataQueries.append(query)
}
奇怪,但事实证明我在 Watch App 的设置中将锻炼省电模式设置为开启。除非我不小心设置它是在我安装 Watch OS 6 时设置为打开的?将其关闭后,心率查询再次正常返回。
我正在使用 HKAnchoredObjectQuery
查询心率,因为我不想为此应用程序使用 WorkoutBuilder API。此查询始终以正常采样率(每秒等)在锻炼期间返回心率,但我注意到在 5 系列手表 OS 6.1 上进行测试时,我只看到非常零星的样本,例如一小时内测得 5 次心率。 HKAnchoredObjectQuery
的文档中没有任何内容表明它已被弃用。知道为什么这种心率收集方法不再有效了吗?
func startHeartRateQuery(from startDate: Date, updateHandler: @escaping ([HKQuantitySample]) -> Void) {
let typeIdentifier = HKQuantityTypeIdentifier.heartRate
startQuery(ofType: typeIdentifier, from: startDate) { _, samples, _, _, error in
guard let quantitySamples = samples as? [HKQuantitySample] else {
print("Heart rate query failed with error: \(String(describing: error))")
return
}
updateHandler(quantitySamples)
}
}
//Generic helper function
private func startQuery(ofType type: HKQuantityTypeIdentifier, from startDate: Date, handler: @escaping
(HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void) {
let datePredicate = HKQuery.predicateForSamples(withStart: startDate, end: nil, options: .strictStartDate)
let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
let queryPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:[datePredicate, devicePredicate])
let quantityType = HKObjectType.quantityType(forIdentifier: type)!
let query = HKAnchoredObjectQuery(type: quantityType, predicate: queryPredicate, anchor: nil,
limit: HKObjectQueryNoLimit, resultsHandler: handler)
query.updateHandler = handler
healthStore.execute(query)
activeDataQueries.append(query)
}
奇怪,但事实证明我在 Watch App 的设置中将锻炼省电模式设置为开启。除非我不小心设置它是在我安装 Watch OS 6 时设置为打开的?将其关闭后,心率查询再次正常返回。