<!--
{
  "availability" : [
    "iOS: 8.0.0 - 9.0.0",
    "iPadOS: 8.0.0 - 9.0.0",
    "macCatalyst: 13.1.0 - 13.1.0",
    "macOS: -",
    "visionOS: 1.0.0 - 1.0.0",
    "watchOS: 2.0.0 - 2.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKAnchoredObjectQuery/init(type:predicate:anchor:limit:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "c:objc(cs)HKAnchoredObjectQuery(im)initWithType:predicate:anchor:limit:completionHandler:"
  },
  "title" : "init(type:predicate:anchor:limit:completionHandler:)"
}
-->

# init(type:predicate:anchor:limit:completionHandler:)

Initializes a new anchored object query.

```
init(type: HKSampleType, predicate: NSPredicate?, anchor: Int, limit: Int, completionHandler handler: @escaping @Sendable (HKAnchoredObjectQuery, [HKSample]?, Int, (any Error)?) -> Void)
```

## Parameters

`type`

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).

`predicate`

A predicate that filters the samples returned by the query.  Pass `nil` to receive all the new samples of the specified type.

`anchor`

The anchor returned by the previous anchored object query. The anchor value corresponds to the last sample that was returned by the previous anchored object query. The new query returns only objects newer than that sample.

`limit`

The maximum number of samples received by the query. To receive all of the new samples, pass [`HKObjectQueryNoLimit`](/documentation/HealthKit/HKObjectQueryNoLimit).

`handler`

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: An array containing the samples returned by this query, or `nil` if an error occurred.
- newAnchor: A value corresponding to the last sample in the results array. Subsequent anchor queries can use this value to receive only the samples that have been saved and the objects that have been deleted since this query completed.
- error: If an error occurs, this parameter contains an object describing the error; otherwise, it is `nil`.

## Return Value

A newly initialized anchor query object.

## Discussion

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

## Topics

### Constants

[`HKAnchoredObjectQueryNoAnchor`](/documentation/HealthKit/HKAnchoredObjectQueryNoAnchor)

An anchor that returns all of the matching samples currently in the HealthKit store.



---

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)