Field notes
HealthKit Sync Identifier and Version: How a Record Gets Replaced Instead of Duplicated
Two metadata keys decide whether a corrected Apple Health entry replaces the old one or sits beside it. Here is how the pair works and what to record.
A HealthKit sync identifier is a string that names one record, and a sync version is a number that says which copy of that record is newest. When an app saves an entry with an identifier HealthKit already holds, and the new version is higher, HealthKit replaces the old entry. You end up with one row, not two.
That matters if you keep a log you plan to audit later. A corrected entry that sits beside the original is two facts where you wanted one. This article covers the two keys, the rule HealthKit applies on save, what happens when the same record arrives twice, and what to write down in your own log.
The two keys and what they hold#
HealthKit defines two metadata keys for this job: HKMetadataKeySyncIdentifier and HKMetadataKeySyncVersion. The identifier is a string. The version is a number, passed as an NSNumber. They are a pair: Apple's documentation says that if you add the identifier to an object's metadata, you must also add the version key.
Both sit in the entry's metadata dictionary, next to any other extra fields the app writes. If you want a refresher on reading those fields, see metadata keys on an Apple Health entry.
The keys have been available since iOS 11 and watchOS 4, so they're not new. Apple's WWDC20 session on syncing health data put them in front of a wider audience, and it describes the purpose this way: the identifier lets HealthKit recognize a sample across any of the user's devices, and the version tells it when that sample has been updated.
What HealthKit does on save#
The rule is short. When an app saves an object that carries a sync identifier, the system looks for existing objects with the same identifier. If it finds a match, it compares the two sync versions. If the new object has a greater version, the system replaces the old object with the new one.
The version page says the same thing from the other side: a newly saved object replaces matching objects with a lower sync version. The replacement also carries through. If the old object belonged to a workout or a correlation, the new one takes its place there too.
Apple's WWDC20 session adds that operations done with sync identifiers are transaction safe. If an error occurs, the data stays in a consistent state.
One limit is worth stating plainly. The documentation spells out the greater-version case. It does not say what happens when the new version is lower than the stored one. Do not build a log process that depends on a guess about that case. Raise the version every time you correct a record, and the documented path is the only one you use.
Same identifier, same version: ignored, not duplicated#
The other half of the story is repetition. Apps that pull data from a server often fetch the same records more than once. Without sync keys, each fetch can save another copy.
The WWDC20 demo shows exactly this. Without the metadata, pressing the fetch button again saved duplicated samples. With both keys set, pressing it repeatedly produced no duplicates. The session also describes the case where a remote server syncs data to both a phone app and a watch app: when the app tries to save a sample that already exists, HealthKit sees it and ignores it.
The session gives a worked example. A physical therapist updates the distance in a six-minute walk test. The sample keeps its identifier, the version goes up, and HealthKit overwrites the earlier sample. The new version then syncs to all of the patient's devices.
Here are the three outcomes for one identifier:
| What arrives | Documented result | | --- | --- | | Same identifier, higher version | The old entry is replaced | | Same identifier, same version | The sample already exists and is ignored | | Same identifier, lower version | Not stated in the documentation |
Why an edit is not really an edit#
HealthKit samples are not edited in place. Without sync identifiers, an app that wants to change an entry has to delete the old sample and save a new one, and the WWDC20 session warns that skipping the delete can leave duplicates. It also notes that an app can only delete data it saved itself. The wider reason is covered in HealthKit samples are immutable.
The identifier and version pair is the tool built for this. It lets an app express "this is the same record, corrected" in a way HealthKit understands, instead of leaving it to a delete and a re-add.
Choosing identifiers you can trust#
An identifier only helps if it stays stable. The WWDC20 session suggests deriving each sample's identifier from a higher-level record identifier. Its example is a weekly report: the report has an identifier, and each sample inside it derives its own from that one, so every sample is uniquely referenced across weeks.
Two habits follow from that:
- Build the identifier from something that does not change when the entry is corrected, such as the ID of the source record.
- Keep the identifier the same across every correction, and change only the version.
A new identifier on a corrected record defeats the whole mechanism. HealthKit sees a different record and saves it beside the first.
What the pair does not cover#
Sync identifiers work inside one app's own records. They do not merge entries that come from different sources. If a phone and a watch each write their own reading of the same moment, that is a separate problem, covered in when two sources record the same thing.
Deletions are a different mechanism too. If you want to know how an app learns that an entry was removed, read how an app finds out about a deletion.
What to keep in your own log#
If an app you use writes to Apple Health, or you keep your own record alongside it, the identifier and version are useful columns. Store them next to each entry. When a row changes, you can see that it is the same record at a newer version, not a second event. A log kept this way, such as the one in OptimizedTracker, holds one line per fact and a trail of which version you are looking at.
Synced data is still only as good as what was entered. The keys stop a second copy from being saved. They say nothing about whether a value is right.
Conclusion#
One identifier per record, a rising version for each correction, and HealthKit keeps one entry. Repeat the same version and it ignores the repeat. Change the identifier and you get a second record. For an auditable log, that is the whole point: the history shows what changed without leaving two versions of the same fact standing side by side.
Share
Related
01
