---
title: "init(type:predicate:anchor:limit:resultsHandler:)"
framework: healthkit
role: symbol
role_heading: Initializer
path: "healthkit/hkanchoredobjectquery/init(type:predicate:anchor:limit:resultshandler:)"
---

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

Initializes a new anchored object query.

## Declaration

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

## Parameters

- `type`: The type of sample to search for. This query supports all subclasses of doc://com.apple.healthkit/documentation/HealthKit/HKSampleType, such as 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.
- `predicate`: A predicate that filters both the samples and the deleted objects returned by the query. Pass nil to receive all the newly added samples and recently deleted objects of the specified type.
- `anchor`: The anchor returned by the previous anchored object query. The anchor object corresponds to the last object that was returned by the previous anchored object query. The new query returns only samples and deleted objects that are newer than that object. Pass nil to receive all the matching samples and recently deleted objects currently in the HealthKit store.
- `limit`: The maximum number of samples received by the query. To receive all of the new samples, pass doc://com.apple.healthkit/documentation/HealthKit/HKObjectQueryNoLimit.
- `handler`: A block that the system calls after gathering the initial results. This block takes the following parameters:

## Mentioned in

Executing Anchored Object Queries

## Return Value

Return Value A newly initialized anchor query object.

## Discussion

Discussion After instantiating the query, call the HKHealthStore class’s execute(_:) method to run it. The queries run on an anonymous background queue. As soon as the query is complete, the results handler block is executed on the same background queue (but not necessarily the same thread). Be sure to dispatch these results to the main queue before updating the user interface. The first time you call this method, pass nil as the anchor parameter. This method returns all matching objects currently in the HealthKit store. Additionally, save the returned anchor object and pass it to the next query. Anchor queries can also act as long-running queries. If you assign an update handler before executing the query, the query continues to monitor the HealthKit store after gathering the initial results. The system calls the update handler on a background queue whenever a matching sample is saved to or deleted from the HealthKit store. To cancel this query, call the store’s stop(_:) method.

## See Also

### Creating Anchored Object Queries

- [Executing Anchored Object Queries](healthkit/executing-anchored-object-queries.md)
- [init(queryDescriptors:anchor:limit:resultsHandler:)](healthkit/hkanchoredobjectquery/init(querydescriptors:anchor:limit:resultshandler:).md)
- [HKObjectQueryNoLimit](healthkit/hkobjectquerynolimit.md)
- [init(type:predicate:anchor:limit:completionHandler:)](healthkit/hkanchoredobjectquery/init(type:predicate:anchor:limit:completionhandler:).md)
