Field notes
HealthKit Samples Are Immutable: Which Entries an App Can Change
A saved HealthKit sample cannot be edited. An app can delete its own samples, or replace one with a sync identifier and a higher sync version.
Short answer#
HealthKit samples are immutable. Once a sample is saved, its properties cannot change. An app can delete the samples it saved itself, and it can replace one by saving a new sample with the same sync identifier and a higher sync version. It can't rewrite an entry in place, and it can't delete entries another app saved.
For a log, that means a correction is never an edit. It is a new record that stands in for an older one, so it helps to plan how you will show that history.
Samples are immutable#
Apple's documentation is direct about this. Of the base class for stored data, it says that HealthKit objects are all immutable: with a few exceptions, such as the object's source revision, the properties are set when the object is first created and they cannot change. The same wording appears for samples, which you set when you create them. Quantity samples, the kind that holds a number and a unit, follow the same rule.
That covers the values you probably care about most: the number, the unit, and the start and end times. None of them can be changed afterward by the app that wrote the sample, or by any other app.
The one stated exception#
The exception Apple names is the source revision. You do not set it. When a sample is saved, HealthKit assigns the source revision itself, and you can read it only on objects retrieved from the store. So even the part that is not frozen at creation is not something your own code edits.
What you can still add at creation#
Immutable doesn't mean bare. The quantity sample documentation says you can extend a sample by adding metadata with custom keys, to save related data your app needs. A key that marks whether the sample was entered by the user is one example. The catch is timing: metadata goes in when you create the sample. If you forgot a field, you cannot add it to the saved entry. For more on what those extra fields look like, see metadata keys on an Apple Health entry.
What an app can delete#
Deleting is the one way to remove a saved sample. Apple's description of deleting objects by type and predicate says it deletes objects saved by this application that match the type and predicate you give it. The discussion puts it plainly: your app can delete only those objects that it previously saved to the HealthKit store.
Two practical points follow.
- Permission matters. If the app has not requested permission to share a data type, the delete fails with an error. If the app has been denied permission, it fails with a different error.
- Ownership matters. Entries written by another app, or by a device, are not yours to delete through the store.
Because the person decides which apps may read and write each type, it is worth knowing where that switch lives. This guide on how to review which apps can read and write your Apple Health data walks through it.
When the permission is revoked#
Here's a trap for any tool that keeps a long log. Apple's documentation notes that if the user revokes sharing permission for an object type, you can no longer delete those objects. They stay in the store, and your app has lost the ability to remove them.
So a correction that depends on deleting the old sample can stop working without a sound. The earlier entries remain, the replacement may not be saved, and nothing on the screen tells anyone. That is the same pattern described in when a permission is switched off, the gap in your log stays silent. If you maintain a log on top of HealthKit, check the permission state before you promise a correction.
Replacing a record with a sync identifier and sync version#
There is a supported way to swap one sample for another. It uses a pair of metadata keys.
A sync identifier is a unique string that identifies a piece of data so it can be updated and synced. If you add it to an object's metadata, you must also add a sync version. A sync version is a number that says which copy is newer.
Here is how Apple describes the behavior when you save a sample that carries both keys:
- The system looks for existing objects with the same sync identifier.
- If it finds a match, it compares the sync versions.
- If the new object has a greater sync version, the system replaces the old object with the new one.
- If the old object belongs to a workout or a correlation, the system also replaces it there.
The new object replaces matching objects that have a lower version. Keep your own version counter steadily increasing, since only a greater version triggers the replacement.
What this means in practice#
You decide the identifier when you first save the sample, so the plan has to exist before the first entry, not after the first mistake. A sample saved without these keys has no identifier to match against, and the only route left for it is deletion followed by a new save, within the limits above.
Entries from other apps#
Everything above describes what your app can do to its own samples. The delete documentation limits deletion to objects your app saved, so an entry written by another app or a device is outside what you can remove. The app name attached to an entry helps you tell sources apart, which is covered in what a device or app name tells you in an entry.
What this means for a correction log#
If saved entries cannot be edited, a trustworthy record keeps its own history of changes.
- Mark a replacement as a new version, not as the original. The old value was real at the time it was saved. Note that it was superseded.
- Record why. A short reason, such as a typo in the unit or a wrong date, is worth more later than a silent swap.
- Keep the identifier and version in your own copy. If you export, those two values let you match a record to its replacement.
- Check permissions first. A correction that needs a delete will fail if sharing was switched off.
In HealthKit, a saved entry is fixed. Changing it means saving a successor, and a good log shows the chain.
Share
Related
01
