# Reading Only What Changed: Anchors in HealthKit Incremental Sync

By The Optimized Team (Product). Published: 2026-11-28T17:47:00.16124+00:00. Updated: 2026-11-28T17:47:00.875794+00:00.
Canonical: https://optimizedtracker.com/blog/healthkit-anchored-object-query-read-only-what-changed

An anchor marks where a HealthKit read stopped. Pass it back next time and the query returns only newer saved or deleted entries. Pass nil and you get everything.

A HealthKit anchored object query returns only the entries saved or deleted since the last time you read, as long as you hand it the anchor from that last read. Give it no anchor and it returns everything of that type. That one difference decides whether an app syncing your Apple Health data stays quick and accurate or re-reads your whole history every time.

Here is what the anchor is, what a nil anchor does, what comes back with each read, and how a long-running query keeps going. I also note what Apple's documentation doesn't say, because a record you plan to audit later shouldn't rest on guesses.

## What an anchor is

An anchor is a bookmark. Apple describes [HKQueryAnchor](https://developer.apple.com/documentation/healthkit/hkqueryanchor) as an object used to identify all the samples previously returned by an anchored object query. The system hands you an anchor in both the results handler and the update handler, and you use it to ask for samples added or deleted after that point.

The query itself, [HKAnchoredObjectQuery](https://developer.apple.com/documentation/healthkit/hkanchoredobjectquery), returns an anchor value that corresponds to the last sample or deleted object it received. A later query can pass that value back to restrict its results to newer saved or deleted objects.

Two details are worth knowing early:

- The query is mostly immutable. You set everything when you create it. The only property you can assign afterward is `updateHandler`.
- `HKQueryAnchor` conforms to `NSSecureCoding`, according to Apple's reference page. The page gives no instructions for storing an anchor between launches. Keeping it somewhere durable is your app's job, and the documentation does not spell out how.

## Nil anchor versus a saved anchor

The first read of a type has no anchor to pass, so you pass nil. Apple's guide to [executing anchored object queries](https://developer.apple.com/documentation/healthkit/executing-anchored-object-queries) says that if the anchor is nil, the query returns all the data of that type currently available in the HealthKit store. On later runs, you pass the anchor returned by the previous query, and the query returns only the changes since the previous run.

That gives a simple pattern for a log you want to keep in step with Apple Health:

1. Run once with a nil anchor and store what comes back.
2. Save the new anchor from the results handler.
3. On the next run, pass the saved anchor.
4. Save the new anchor again.

Step 4 is the one people skip. Apple's example assigns the new anchor inside the handler on every call. If you keep the first anchor forever, each read repeats the work since that first point, and your copy may count the same entry twice.

A nil anchor is not only for first runs. Apple notes that a single anchored object query is often more efficient than running separate sample and observer queries, so you may use one even when you do not want anchors to limit the results. In that case you set the anchor to nil.

## What comes back with the anchor

The results handler receives five things: the query, an optional array of samples, an optional array of deleted objects, the new anchor and an optional error. The samples are what was added. The deleted objects are what was removed. The anchor is your new bookmark.

A deleted object, an instance of [HKDeletedObject](https://developer.apple.com/documentation/healthkit/hkdeletedobject), carries the UUID of the entry that was removed and optional metadata. If your own database keeps a copy of each entry, the UUID is how you find the copy to remove.

Don't lean on this too hard. Apple states that deleted objects are temporary and the system may remove them from the store at any time to free up space. To be sure you hear about every deletion, Apple points to an observer query registered for background delivery, which wakes the app on a change and lets you run an anchored query for the newly deleted objects. We walk through that in [how an app finds out about a deleted entry](/blog/healthkit-deleted-objects-how-an-app-finds-out).

Results also arrive on an anonymous background queue. Apple's example reminds you to move to the main queue before touching the interface.

## One read or a running query

By default the query runs once. Apple's guide says it runs on an anonymous background queue when you call the store's `execute` method, and the results handler fires when it finishes.

If you assign an update handler, the query becomes long-running. After returning the initial results, it keeps monitoring the HealthKit store and calls your handler whenever it detects a change to the affected objects. The update handler receives samples, deleted objects and an anchor in the same shape as the results handler, so the same rule applies: store the newest anchor each time it arrives.

Which one fits depends on the job. A tracker that reconciles once when it opens can run a single read. One that should reflect changes while it stays open can attach an update handler.

## What the anchor does not tell you

An anchor says where you stopped. It does not say why something changed, and it does not describe the rest of your records. If a person edits what they logged somewhere else, your copy only learns what the store reports. Entries in HealthKit follow their own rules, which we cover in [HealthKit samples are immutable](/blog/healthkit-samples-immutable-which-entries-an-app-can-change) and in [the sync identifier and version](/blog/healthkit-sync-identifier-version-replace-instead-of-duplicate).

Apple's pages for these classes also leave several points open. They do not describe how the `limit` parameter interacts with an anchor beyond defining `HKObjectQueryNoLimit` as a value that returns all matching samples. They do not give steps for archiving an anchor. If your sync depends on either, test it against your own data rather than assuming.

## What to record

If you keep your own log alongside Apple Health, a few fields make later audits easier:

- The date and time of each sync run, so you can tell which read produced which rows.
- The sample type each anchor belongs to. An anchor comes from a query for particular types, so keep them apart.
- Whether the run started from a nil anchor or a saved one.
- The UUID of every entry you imported, so a later deletion can be matched.

None of this makes the data more accurate. It makes the history of how your copy was built easy to follow. That matters when a number looks wrong months later and you need to know whether it came from a full read or an incremental one.
