Search code examples
ioshealthkit

How to prevent writing duplicate data in HealthKit?


I am trying to integrate HealthKit in my app, but am getting duplicate data anytime the user syncs/opens the app, etc. I am writing the data on app open, but currently have no way of storing whether the data was written in my app. I was hoping that there was a way to do this in HealthKit. I looked at setting the UUID, but that's a get-only property.

I also looked at the External UUID metadata key, but that doesn't prevent duplicates. Is there anyway to do this other than creating a separate field in my app to note whether or not a specific piece of data was written to HealthKit?


Solution

  • I'd encourage you to implement some kind of tracking for what you've already written to HealthKit to avoid the inefficiency of rewriting redundant data on every app launch. However, you can also use HKMetadataKeySyncIdentifier to uniquely identify samples in HealthKit and have them de-duplicated for you automatically.