首页 > 解决方案 > Can you retrieve different HKQuantity sources together?

问题描述

As a disclaimer, I am new to swift and am mainly relying on other coding experience. I am working on an app that predicts the blood glucose drop of a Type 1 Diabetic after exercise. This requires the input of multiple different Healthkit quantities, including active calories, insulin, and blood glucose, but I have had trouble fetching these quantities to do math with them in the same place. I am working on the foundation of SpeedySloth, Apple's example workout app, and this is how they retrieve Healthkit data:

case HKQuantityType.quantityType(forIdentifier: .heartRate):
                /// - Tag: SetLabel
                let heartRateUnit = HKUnit.count().unitDivided(by: HKUnit.minute())
                let value = statistics.mostRecentQuantity()?.doubleValue(for: heartRateUnit)
                let roundedValue = Double( round( 1 * value! ) / 1 )
                label.setText("\(roundedValue) BPM")
            case HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned):
                let energyUnit = HKUnit.kilocalorie()
                let value = statistics.sumQuantity()?.doubleValue(for: energyUnit)
                let roundedValue = Double( round( 1 * value! ) / 1 )
label.setText("\(roundedValue) cal\n")

As you can see, a new case is created every time a quantity is recalled, and each one is called separately. However, is there any way to call them together, or multiple at once, so I can work with the quantities? I tried other methods, including a Predicate within one of the cases, but that did not seem to work. Any suggestions would be appreciated.

Since you cannot query for multiple quantities within the same query, is it possible to nest a predicate query within the quantity recall so that I could work with both variables? Here is what I tried to put a predicate query within the active energy case:

                let energyUnit = HKUnit.kilocalorie()
                let value = statistics.sumQuantity()?.doubleValue(for: energyUnit)
                let roundedValue = Double( round( 1 * value! ) / 1 )



                let quantityType = HKObjectType.quantityType(forIdentifier:(HKQuantityTypeIdentifier.bloodGlucose))
                let mostRecentPredicate = HKQuery.predicateForSamples(withStart: Date.distantPast,
                                                                      end: Date(),
                                                                      options: .strictEndDate)

                let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate,
                                                      ascending: false)

                let limit = 1

                let sampleQuery = HKSampleQuery(sampleType: quantityType!,
                                                predicate: mostRecentPredicate,
                                                limit: limit,
                                                sortDescriptors: [sortDescriptor]) { (query, samples, error) in


                let sample = samples?.first as? HKQuantitySample
                let BG = sample?.quantity
                let ESF = 0.125
                let TotBGChg = -1 * ESF * roundedValue
                let roundedTBGC = round(TotBGChg)
                label.setText("\(roundedValue) cal\n\(String(describing: BG)) mg/dL")
                }
                    return

标签: iosswiftiphonepredicatehealthkit

解决方案


推荐阅读