Я пытаюсь считать в реальном времени данные о частоте пульса и вариабельности пульса с яблочных часов. Я могу прочитать значение сердечного ритма, но не знаю, как реализовать считывание ВСР. Можно ли считать их одновременно?
class HealthKitManager {
private var healthStore = HKHealthStore()
private var heartRateQuantity = HKUnit(from: "count/min")
private var heartRateVariability = HKUnit(from: "count/min")
private var activeQueries = [HKQuery]()
@Published var heartRateValues = HeartRateValues()
func autorizeHealthKit() {
let heartRate = HKObjectType.quantityType(forIdentifier: .heartRate)!
let heartRateVariability = HKObjectType.quantityType(forIdentifier: .heartRateVariabilitySDNN)!
let HKreadTypes: Set = [heartRate, heartRateVariability]
healthStore.requestAuthorization(toShare: nil, read: HKreadTypes) { (success, error) in
if let error = error {
print("Error requesting health kit authorization: \(error)")
}
}
}
func fetchHeartRateData(quantityTypeIdentifier: HKQuantityTypeIdentifier ) {
let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
let updateHandler: (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void = {
query, samples, deletedObjects, queryAnchor, error in
guard let samples = samples as? [HKQuantitySample] else {
return
}
self.process(samples, type: quantityTypeIdentifier)
}
let query = HKAnchoredObjectQuery(type: HKObjectType.quantityType(forIdentifier: quantityTypeIdentifier)!, predicate: devicePredicate, anchor: nil, limit: HKObjectQueryNoLimit, resultsHandler: updateHandler)
query.updateHandler = updateHandler
healthStore.execute(query)
activeQueries.append(query)
}
private func process(_ samples: [HKQuantitySample], type: HKQuantityTypeIdentifier) {
for sample in samples {
if type == .heartRate {
DispatchQueue.main.async {
self.heartRateValues.heartRate = sample.quantity.doubleValue(for: self.heartRateQuantity)
}
}
// Not Sure about this part and readings show 0.0
else if type == .heartRateVariabilitySDNN {
DispatchQueue.main.async {
self.heartRateValues.heartRateVariability = sample.quantity.doubleValue(for: self.heartRateVariability)
}
}
}
}
func stopFetchingHeartRateData() {
activeQueries.forEach { healthStore.stop($0) }
activeQueries.removeAll()
DispatchQueue.main.async {
self.heartRateValues.heartRate = 0.0
self.heartRateValues.heartRateVariability = 0.0
}
}
}
Здесь был задан сопоставимый вопрос, но он не получил ответа: Получить Apple watch heartRateVariabilitySDNN в реальном времени?