<!--
{
  "availability" : [
    "iOS: 15.4.0 -",
    "iPadOS: 15.4.0 -",
    "macCatalyst: 15.4.0 -",
    "macOS: 13.0.0 -",
    "visionOS: -",
    "watchOS: 8.5.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKAnchoredObjectQueryDescriptor",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "s:9HealthKit31HKAnchoredObjectQueryDescriptorV"
  },
  "title" : "HKAnchoredObjectQueryDescriptor"
}
-->

# HKAnchoredObjectQueryDescriptor

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

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

## Overview

Use  [`HKAnchoredObjectQueryDescriptor`](/documentation/HealthKit/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`](/documentation/HealthKit/HKAnchoredObjectQueryDescriptor) instances.
- Monitor the HealthKit store for any changes to the matching data using a long-running [`HKAnchoredObjectQueryDescriptor`](/documentation/HealthKit/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`](/documentation/HealthKit/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.

```swift
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 ``doc://com.apple.healthkit/documentation/HealthKit/HKQueryAnchor`` instances adopt the <doc://com.apple.documentation/documentation/Foundation/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`](/documentation/HealthKit/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:)`](/documentation/HealthKit/HKAnchoredObjectQueryDescriptor/results(for:)) method to start your long-running query. This method returns an <doc://com.apple.documentation/documentation/Swift/AsyncSequence> instance which HealthKit uses to return `Element` instances. The first result represents any changes currently in the HealthKit store, and additional results represent changes as they occur.

```swift
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: [HKSamplePredicate<Sample>], anchor: HKQueryAnchor?, limit: Int?)`](/documentation/HealthKit/HKAnchoredObjectQueryDescriptor/init(predicates:anchor:limit:))

Creates an anchored object query descriptor.

### Running Queries

[`func result(for: HKHealthStore) async throws -> HKAnchoredObjectQueryDescriptor<Sample>.Result`](/documentation/HealthKit/HKAnchoredObjectQueryDescriptor/result(for:))

Runs a one-shot query and asynchronously returns a snapshot of the current matching results.

[`func results(for: HKHealthStore) -> HKAnchoredObjectQueryDescriptor<Sample>.Results`](/documentation/HealthKit/HKAnchoredObjectQueryDescriptor/results(for:))

Initiates a long-running query that returns its results using an asynchronous sequence.

[`struct Result`](/documentation/HealthKit/HKAnchoredObjectQueryDescriptor/Result)

A set of results from an anchored object query.

[`struct Results`](/documentation/HealthKit/HKAnchoredObjectQueryDescriptor/Results)

An asynchronous sequence that emits updates from an anchored object query.

### Accessing Query Properties

[`var predicates: [HKSamplePredicate<Sample>]`](/documentation/HealthKit/HKAnchoredObjectQueryDescriptor/predicates)

A predicate that limits the results that the query returns.

[`var anchor: HKQueryAnchor?`](/documentation/HealthKit/HKAnchoredObjectQueryDescriptor/anchor)

An anchor that a previous anchored object query returned.

[`var limit: Int?`](/documentation/HealthKit/HKAnchoredObjectQueryDescriptor/limit)

The maximum number of samples that the query returns.

### Default Implementations

[HKAsyncQuery Implementations](/documentation/HealthKit/HKAnchoredObjectQueryDescriptor/HKAsyncQuery-Implementations)

[HKAsyncSequenceQuery Implementations](/documentation/HealthKit/HKAnchoredObjectQueryDescriptor/HKAsyncSequenceQuery-Implementations)

## Relationships

### Conforms To

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

[`Sendable`](/documentation/Swift/Sendable)

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

[`Copyable`](/documentation/Swift/Copyable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Escapable`](/documentation/Swift/Escapable)

---

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)