Search code examples
iosswiftswift3healthkit

Compare HKSample with another HKSample to prevent duplicates


[Swift 3.0]

I am trying to retrieve an HKSample from HealthKit to add data to my application and would also like to update the application whenever HealthKit is.

I have added an HKObserverQuery and it works. However, it does not check whether the HKSample already exists in my application before it adds it and continuously adds the sample to my application.

How can I check if an HKSample with identical properties already exist?


Solution

  • HKSample inherits from HKObject.

    All HKObject instances have a uuid property (in Swift) to uniquely identify that object.

    So, when you query, you'll need to compare what you have already with what's new by uuid. Any you don't find in what you have already is new.

    Docs are here.