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

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

Instantiates and returns a sample query.

```
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 [`HKCategoryType`](/documentation/HealthKit/HKCategoryType), [`HKCorrelationType`](/documentation/HealthKit/HKCorrelationType), [`HKQuantityType`](/documentation/HealthKit/HKQuantityType), or [`HKWorkoutType`](/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 [`HKObjectQueryNoLimit`](/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:

- query: A reference to the query that called this block.
- results: An array containing the samples found by the query, or `nil` if an error occurs.
- error: If an error occurs, this parameter contains an object describing the error. Otherwise, its value 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 background queue. You typically dispatch these results to the main queue to update the user interface.

---

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)