<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKSourceQuery/init(sampleType:samplePredicate:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "c:objc(cs)HKSourceQuery(im)initWithSampleType:samplePredicate:completionHandler:"
  },
  "title" : "init(sampleType:samplePredicate:completionHandler:)"
}
-->

# init(sampleType:samplePredicate:completionHandler:)

Instantiates and returns a source query.

```
init(sampleType: HKSampleType, samplePredicate objectPredicate: NSPredicate?, completionHandler: @escaping @Sendable (HKSourceQuery, Set<HKSource>?, (any Error)?) -> Void)
```

## Parameters

`sampleType`

The type of sample to search for. This query supports all sample types. Specifically, you can pass any concrete subclass of the [`HKSampleType`](/documentation/HealthKit/HKSampleType) class (the [`HKQuantityType`](/documentation/HealthKit/HKQuantityType), [`HKCategoryType`](/documentation/HealthKit/HKCategoryType), [`HKWorkoutType`](/documentation/HealthKit/HKWorkoutType),  and [`HKCorrelationType`](/documentation/HealthKit/HKCorrelationType) classes).

`objectPredicate`

A predicate that limits the samples matched by the query. Pass `nil` if you want to receive the sources for all the samples of the specified type.

`completionHandler`

A block that is called when the query finishes executing. This block takes the following parameters:

- query: A reference to the query calling this block.
- results: A set containing the sources for all the samples that match both the sample type and the object predicate, or `nil` if an error occurred.
- error: If an error occurs, this parameter contains an object describing the error; otherwise, it is `nil`.

## Return Value

A newly initialized sample query object.

## Discussion

After instantiating the query, call the [`HKHealthStore`](/documentation/HealthKit/HKHealthStore) class’s [`execute(_:)`](/documentation/HealthKit/HKHealthStore/execute(_:)) method to run this query. Queries run on an anonymous background queue. As soon as the query is complete, the results handler is executed on the same background queue (but not necessarily on the same thread).

---

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)