---
title: HKAnchoredObjectQueryDescriptor
framework: healthkit
role: symbol
role_heading: Structure
path: healthkit/hkanchoredobjectquerydescriptor
---

# HKAnchoredObjectQueryDescriptor

A query interface that runs anchored object queries using Swift concurrency.

## Declaration

```swift
struct HKAnchoredObjectQueryDescriptor<Sample> where Sample : HKSample
```

## Mentioned in

Running Queries with Swift Concurrency Reading data from HealthKit

## Overview

Overview Use  HKAnchoredObjectQueryDescriptor to read any changes to the HealthKit store that occurred after the provided anchor. When creating a new query descriptor, if you pass nil as the anchor parameter, the query reads all matching data from the store. There are two common use cases for anchored object queries: Batch-read all the matching data from the HealthKit store using a series of HKAnchoredObjectQueryDescriptor instances. Monitor the HealthKit store for any changes to the matching data using a long-running HKAnchoredObjectQueryDescriptor instance. Batch Read Existing Data Users may have large quantities of data saved to the HealthKit store; therefore, reading all data for a given data type might become very expensive, both in terms of memory usage and processing time. To avoid performance issues, you can use HKAnchoredObjectQueryDescriptor queries to read the data in batches. Start with a nil-valued anchor, and create a one-shot query descriptor that reads a batch of data. After you process the results from one query, start a new one-shot query for the next batch. Continue reading batches until there’s no new data. let stepType = HKQuantityType(.stepCount)

// Start by reading all matching data. var anchor: HKQueryAnchor? = nil var results: HKAnchoredObjectQueryDescriptor<HKQuantitySample>.Result

repeat {     // Create a query descriptor that reads a batch     // of 100 matching samples.     let anchorDescriptor =     HKAnchoredObjectQueryDescriptor(         predicates: [.quantitySample(type: stepType)],         anchor: anchor,         limit: 100     )

results = try await anchorDescriptor.result(for: store)     anchor = results.newAnchor          // Process the batch of results here.      } while (results.addedSamples != []) && (results.deletedObjects != []) tip: Because HKQueryAnchor instances adopt the NSSecureCoding protocol, you can save the most recent anchor and use it the next time your app launches. Monitor for Changes To monitor the HealthKit store for changes, start by creating an HKAnchoredObjectQueryDescriptor instance that matches the data you want to monitor. Pass in the anchor from the last time you read data from the HealthKit store. Next, call the query descriptor’s results(for:) method to start your long-running query. This method returns an AsyncSequence instance which HealthKit uses to return Results/Element instances. The first result represents any changes currently in the HealthKit store, and additional results represent changes as they occur. let stepType = HKQuantityType(.stepCount)

// Create a query descriptor. let anchorDescriptor = HKAnchoredObjectQueryDescriptor(     predicates: [.quantitySample(type: stepType)],     anchor: anchor )

let updateQueue = anchorDescriptor.results(for: store)

updateTask = Task {     for try await update in updateQueue {         // Process the update here.         print(update)     } }

## Topics

### Creating Query Descriptors

- [init(predicates:anchor:limit:)](healthkit/hkanchoredobjectquerydescriptor/init(predicates:anchor:limit:).md)

### Running Queries

- [result(for:)](healthkit/hkanchoredobjectquerydescriptor/result(for:).md)
- [results(for:)](healthkit/hkanchoredobjectquerydescriptor/results(for:).md)
- [HKAnchoredObjectQueryDescriptor.Result](healthkit/hkanchoredobjectquerydescriptor/result.md)
- [HKAnchoredObjectQueryDescriptor.Results](healthkit/hkanchoredobjectquerydescriptor/results.md)

### Accessing Query Properties

- [predicates](healthkit/hkanchoredobjectquerydescriptor/predicates.md)
- [anchor](healthkit/hkanchoredobjectquerydescriptor/anchor.md)
- [limit](healthkit/hkanchoredobjectquerydescriptor/limit.md)

### Default Implementations

- [HKAsyncQuery Implementations](healthkit/hkanchoredobjectquerydescriptor/hkasyncquery-implementations.md)
- [HKAsyncSequenceQuery Implementations](healthkit/hkanchoredobjectquerydescriptor/hkasyncsequencequery-implementations.md)

## Relationships

### Conforms To

- [Copyable](swift/copyable.md)
- [Escapable](swift/escapable.md)
- [HKAsyncQuery](healthkit/hkasyncquery.md)
- [HKAsyncSequenceQuery](healthkit/hkasyncsequencequery.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Long-running queries

- [HKActivitySummaryQueryDescriptor](healthkit/hkactivitysummaryquerydescriptor.md)
- [HKActivitySummaryQuery](healthkit/hkactivitysummaryquery.md)
- [HKAnchoredObjectQuery](healthkit/hkanchoredobjectquery.md)
- [HKObserverQuery](healthkit/hkobserverquery.md)
