Contents

HKElectrocardiogram

A sample for electrocardiogram data.

Declaration

class HKElectrocardiogram

Overview

An HKElectrocardiogram is a collection of voltage values representing waveforms from one or more leads. The HKElectrocardiogram sample provides high-level details about the ECG reading, such as the sampling frequency or classification. HealthKit provides read-only access to electrocardiogram (ECG) data saved by Apple Watch.

You can query for HKElectrocardiogram samples using an HKSampleQuery.

// Create the electrocardiogram sample type.
let ecgType = HKObjectType.electrocardiogramType()


// Query for electrocardiogram samples
let ecgQuery = HKSampleQuery(sampleType: ecgType,
                             predicate: nil,
                             limit: HKObjectQueryNoLimit,
                             sortDescriptors: nil) { (query, samples, error) in
    if let error = error {
        // Handle the error here.
        fatalError("*** An error occurred \(error.localizedDescription) ***")
    }
    
    guard let ecgSamples = samples as? [HKElectrocardiogram] else {
        fatalError("*** Unable to convert \(String(describing: samples)) to [HKElectrocardiogram] ***")
    }
    
    for sample in ecgSamples {
        // Handle the samples here.
        
    }
}

// Execute the query.
healthStore.execute(ecgQuery)

After retrieving an HKElectrocardiogram sample, you can access the voltage measurements associated with the sample use an HKElectrocardiogramQuery query.

// Create a query for the voltage measurements
let voltageQuery = HKElectrocardiogramQuery(ecgSample) { (query, result) in
    switch(result) {
    
    case .measurement(let measurement):
        if let voltageQuantity = measurement.quantity(for: .appleWatchSimilarToLeadI) {
            // Do something with the voltage quantity here.

        }
    
    case .done:
        // No more voltage measurements. Finish processing the existing measurements.

    case .error(let error):
        // Handle the error here.

    }
}

// Execute the query.
healthStore.execute(voltageQuery)

Topics

Accessing Overview Information

Accessing Voltage Measurements

Specifying Metadata

Specifying Predicate Key Paths

See Also

Electrocardiograms