---
title: "init(sampleType:samplePredicate:completionHandler:)"
framework: healthkit
role: symbol
role_heading: Initializer
path: "healthkit/hksourcequery/init(sampletype:samplepredicate:completionhandler:)"
---

# init(sampleType:samplePredicate:completionHandler:)

Instantiates and returns a source query.

## Declaration

```swift
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 doc://com.apple.healthkit/documentation/HealthKit/HKSampleType class (the doc://com.apple.healthkit/documentation/HealthKit/HKQuantityType, doc://com.apple.healthkit/documentation/HealthKit/HKCategoryType, doc://com.apple.healthkit/documentation/HealthKit/HKWorkoutType, and doc://com.apple.healthkit/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:

## Mentioned in

Executing Source Queries

## Return Value

Return Value A newly initialized sample query object.

## Discussion

Discussion After instantiating the query, call the HKHealthStore class’s 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).

## See Also

### Creating Source Queries

- [Executing Source Queries](healthkit/executing-source-queries.md)
