---
title: "init(sampleType:predicate:limit:sortDescriptors:resultsHandler:)"
framework: healthkit
role: symbol
role_heading: Initializer
path: "healthkit/hksamplequery/init(sampletype:predicate:limit:sortdescriptors:resultshandler:)"
---

# init(sampleType:predicate:limit:sortDescriptors:resultsHandler:)

Instantiates and returns a sample query.

## Declaration

```swift
init(sampleType: HKSampleType, predicate: NSPredicate?, limit: Int, sortDescriptors: [NSSortDescriptor]?, resultsHandler: @escaping @Sendable (HKSampleQuery, [HKSample]?, (any Error)?) -> Void)
```

## Parameters

- `sampleType`: The type of sample to search for. This object can be an instance of the doc://com.apple.healthkit/documentation/HealthKit/HKCategoryType, doc://com.apple.healthkit/documentation/HealthKit/HKCorrelationType, doc://com.apple.healthkit/documentation/HealthKit/HKQuantityType, or doc://com.apple.healthkit/documentation/HealthKit/HKWorkoutType class.
- `predicate`: A predicate that limits the results returned by the query. Pass nil to receive all the samples of the specified type.
- `limit`: The maximum number of samples returned by the query. If you want to return all matching samples, use doc://com.apple.healthkit/documentation/HealthKit/HKObjectQueryNoLimit.
- `sortDescriptors`: An array of sort descriptors that specify the order of the results returned by this query. Pass nil if you don’t need the results in a specific order. note: HealthKit defines a number of sort identifiers (for example, doc://com.apple.healthkit/documentation/HealthKit/HKSampleSortIdentifierStartDate and doc://com.apple.healthkit/documentation/HealthKit/HKWorkoutSortIdentifierDuration). Use the sort descriptors you create with these identifiers only in queries. You cannot use them to perform an in-memory sort of an array of samples.
- `resultsHandler`: A block that is called when the query finishes executing. This block takes the following parameters:

## Mentioned in

Executing Sample 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 background queue. You typically dispatch these results to the main queue to update the user interface.

## See Also

### Creating Sample Queries

- [Executing Sample Queries](healthkit/executing-sample-queries.md)
- [init(queryDescriptors:limit:resultsHandler:)](healthkit/hksamplequery/init(querydescriptors:limit:resultshandler:).md)
- [init(queryDescriptors:limit:sortDescriptors:resultsHandler:)](healthkit/hksamplequery/init(querydescriptors:limit:sortdescriptors:resultshandler:).md)
- [HKObjectQueryNoLimit](healthkit/hkobjectquerynolimit.md)
- [HealthKit sort descriptors](healthkit/healthkit-sort-descriptors.md)
