<!--
{
  "availability" : [
    "iOS: 14.0.0 -",
    "iPadOS: 14.0.0 -",
    "macCatalyst: 14.0.0 -",
    "macOS: 13.0.0 -",
    "visionOS: -",
    "watchOS: 7.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKElectrocardiogramQuery/init(_:dataHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "s:So24HKElectrocardiogramQueryC9HealthKitE_11dataHandlerABSo0A0C_yAB_AbCE6ResultOtctcfc"
  },
  "title" : "init(_:dataHandler:)"
}
-->

# init(_:dataHandler:)

Creates a new electrocardiogram query object.

```
convenience init(_ ecg: HKElectrocardiogram, dataHandler: @escaping (HKElectrocardiogramQuery, HKElectrocardiogramQuery.Result) -> Void)
```

## Parameters

`ecg`

The electrocardiogram sample whose voltages you want to access.

`dataHandler`

A block that the query calls repeatedly to return the voltage data. The handler takes the following parameters:

- `query`: The query that returned the results.
- result: An enumeration that contains a result value.

## Discussion

When you run the query, it calls the data handler once for each voltage measurement, passing a [`HKElectrocardiogramQuery.Result.measurement(_:)`](/documentation/HealthKit/HKElectrocardiogramQuery/Result/measurement(_:)) instance that contains the voltage data. After it has sent all the voltage measurements, it calls the data handler one last time, passing [`HKElectrocardiogramQuery.Result.done`](/documentation/HealthKit/HKElectrocardiogramQuery/Result/done). If an error occurs, it stops collecting voltage data and passes [`HKElectrocardiogramQuery.Result.error(_:)`](/documentation/HealthKit/HKElectrocardiogramQuery/Result/error(_:)) instead.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)