【发布时间】:2019-10-28 11:13:59
【问题描述】:
我使用HKAnchoredObjectQuery 来查询心率,因为我不想为此应用使用 WorkoutBuilder API。此查询始终以正常采样率(每秒等)在锻炼期间返回心率,但我注意到在 Series 5 Watch 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)
}
【问题讨论】:
标签: swift apple-watch healthkit watchos heartrate