2

I'm trying to get the individual voltage measurements of an ECG back from Apple HealthKit using new APIs in iOS 14.

I've already managed to use:

let ecgQuery = HKSampleQuery(sampleType: HKObjectType.electrocardiogramType(), predicate: samplePredicate, limit: 0, sortDescriptors: [sortDescriptor]){ (query, results, error) in

which gets me a HKElectrocardiogram object. From this I can see the average heart rate, ECG classification etc...

I now believe I need to pass that object into an HKElectrocardiogramQuery like this:

let ecgSample = HKElectrocardiogramQuery(ecg) { (query, result) in

but I can't find any way to extract data from the result data handler. If I put a print in on result, it executes many times but again, I can't extract the data. result is of type HKElectrocardiogramQuery.Result

The documentations pretty sketchy on Apple's developer site with zero examples provided. The capability is mentioned though in Apple's What's New In HealthKit talk from WWDC 2020. Any help would be very appreciated.

Cheers

Simon
  • 304
  • 2
  • 17

2 Answers2

4

Based on the available documentation, you have to switch over the result to get the measurement value

let query = HKElectrocardiogramQuery(ecg) { (query, result) in

    switch result {
    case .error(let error):
        print("error: ", error)
        
    case .measurement(let value):
        print("value: ", value)
        
    case .done:
        print("done")
    }
}

store.execute(query)
Jithin
  • 913
  • 5
  • 6
  • Thanks, I'd worked out the switch bit, but haven't used that syntax before for the "let value" bit. Works great though. – Simon Aug 12 '20 at 23:20
4

in case anyone wants the full sample code here it is:

if #available(iOS 14.0, *) {
        let predicate = HKQuery.predicateForSamples(withStart: Date.distantPast,end: Date.distantFuture,options: .strictEndDate)
        let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
        let ecgQuery = HKSampleQuery(sampleType: HKObjectType.electrocardiogramType(), predicate: predicate, limit: 0, sortDescriptors: [sortDescriptor]){ (query, samples, error) in
            guard let samples = samples,
                let mostRecentSample = samples.first as? HKElectrocardiogram else {
                return
            }
            print(mostRecentSample)
            var ecgSamples = [(Double,Double)] ()
            let query = HKElectrocardiogramQuery(mostRecentSample) { (query, result) in
                
                switch result {
                case .error(let error):
                    print("error: ", error)
                    
                case .measurement(let value):
                    print("value: ", value)
                    let sample = (value.quantity(for: .appleWatchSimilarToLeadI)!.doubleValue(for: HKUnit.volt()) , value.timeSinceSampleStart)
                    ecgSamples.append(sample)
                    
                case .done:
                    print("done")
                }
            }
            self.healthMonitor.healthStore.execute(query)
        }
        
        
        healthMonitor.healthStore.execute(ecgQuery)
    } else {
        // Fallback on earlier versions
    }
mkhoshpour
  • 845
  • 1
  • 10
  • 22