ios – Why am I not getting any returned values from my HKStatisticsQuery?


I’m creating an IOS utility meant to fetch sure well being factors, accumaltiive and common, over a sure time period. The time is recieved through a timer begin and a timer finish, and as soon as the timer is topped the accrued information factors will get saved to a view. Whereas the timer is actively working, I would really like the newest well being factors to shocase on the UI. I’m attempting to make use of these time factors in a predicate to make use of within the querey however nothing is being returned after I take a look at the appliance and I’m left with solely 0’s. Might it’s a problem with how my querey is about up?

    func updateHealthData(isFinalFetch: Bool = false) {
        let endDate = Date()
        guard let startDate = startTime else { return }
        let predicate = HKQuery.predicateForSamples(withStart: startDate, finish: endDate, choices: .strictEndDate)
        
        let dispatchGroup = DispatchGroup()
        
        dispatchGroup.enter()
        fetchStepsData(predicate: predicate) { dispatchGroup.depart() }
        
        dispatchGroup.enter()
        fetchHeartRateData(predicate: predicate) { dispatchGroup.depart() }
        
        dispatchGroup.enter()
        fetchWristTemperatureData(predicate: predicate) { dispatchGroup.depart() }
        
        dispatchGroup.enter()
        fetchBloodOxygenData(predicate: predicate) { dispatchGroup.depart() }
        
        dispatchGroup.enter()
        fetchRespiratoryRateData(predicate: predicate) { dispatchGroup.depart() }
        
        dispatchGroup.notify(queue: .essential) {
            if isFinalFetch {
                // Retailer remaining session information if that is the final fetch
                activityController.addRecordedSession(
                    activityTitle: exercise.title,
                    totalTime: elapsedTime,
                    accelerometerData: accelerometerData,
                    gyroscopeData: gyroscopeData,
                    gravityData: gravityData,
                    orientationData: orientationData,
                    heartRate: heartRateAverage,
                    stepCount: stepCountTotal,
                    wristTemperature: wristTemperatureAverage,
                    bloodOxygenLevel: bloodOxygenAverage,
                    respiratoryRate: respiratoryRateAverage
                )
            }
        }
    }
     
     // Fetching Capabilities
     func fetchStepsData(predicate: NSPredicate, completion: @escaping () -> Void) {
         let stepType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
         let question = HKStatisticsQuery(quantityType: stepType, quantitySamplePredicate: predicate, choices: .cumulativeSum) { _, end result, _ in
             if let sum = end result?.sumQuantity() {
                 DispatchQueue.essential.async {
                     self.stepCountTotal = sum.doubleValue(for: HKUnit.depend())
                 }
             }
             completion()
         }
         healthKitManager.healthStore.execute(question)
     }
     
     func fetchHeartRateData(predicate: NSPredicate, completion: @escaping () -> Void) {
         let heartRateType = HKQuantityType.quantityType(forIdentifier: .heartRate)!
         let question = HKStatisticsQuery(quantityType: heartRateType, quantitySamplePredicate: predicate, choices: .discreteAverage) { _, end result, _ in
             if let avg = end result?.averageQuantity() {
                 DispatchQueue.essential.async {
                     self.heartRateAverage = avg.doubleValue(for: HKUnit(from: "depend/min"))
                 }
             }
             completion()
         }
         healthKitManager.healthStore.execute(question)
     }
     
     func fetchWristTemperatureData(predicate: NSPredicate, completion: @escaping () -> Void) {
         let wristTempType = HKQuantityType.quantityType(forIdentifier: .bodyTemperature)!
         let question = HKStatisticsQuery(quantityType: wristTempType, quantitySamplePredicate: predicate, choices: .discreteAverage) { _, end result, _ in
             if let avg = end result?.averageQuantity() {
                 DispatchQueue.essential.async {
                     self.wristTemperatureAverage = avg.doubleValue(for: HKUnit.degreeCelsius())
                 }
             }
             completion()
         }
         healthKitManager.healthStore.execute(question)
     }
     
     func fetchBloodOxygenData(predicate: NSPredicate, completion: @escaping () -> Void) {
         let oxygenType = HKQuantityType.quantityType(forIdentifier: .oxygenSaturation)!
         let question = HKStatisticsQuery(quantityType: oxygenType, quantitySamplePredicate: predicate, choices: .discreteAverage) { _, end result, _ in
             if let avg = end result?.averageQuantity() {
                 DispatchQueue.essential.async {
                     self.bloodOxygenAverage = avg.doubleValue(for: HKUnit.p.c()) * 100
                 }
             }
             completion()
         }
         healthKitManager.healthStore.execute(question)
     }
     
     func fetchRespiratoryRateData(predicate: NSPredicate, completion: @escaping () -> Void) {
         let respiratoryRateType = HKQuantityType.quantityType(forIdentifier: .respiratoryRate)!
         let question = HKStatisticsQuery(quantityType: respiratoryRateType, quantitySamplePredicate: predicate, choices: .discreteAverage) { _, end result, _ in
             if let avg = end result?.averageQuantity() {
                 DispatchQueue.essential.async {
                     self.respiratoryRateAverage = avg.doubleValue(for: HKUnit.depend().unitDivided(by: HKUnit.minute()))
                 }
             }
             completion()
         }
         healthKitManager.healthStore.execute(question)
    }

Variables not being up to date in an lively timer session
Saved remaining values with no information being showcased

I’ve tried straight fetching lively coronary heart fee and showcasing that to see whether it is my well being authurization thats a problem however it isn’t the case since I’m able to run that querey.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles