4

Hope this question doesn't sound too silly, but I haven't been able to find a solution yet. I'm currently writing an app using Xamarin that integrates with HealthKit. Data stored in HK is periodically synced to a server. This is done through an HKAnchoredObjectQuery that pulls Food Correlations.

Since the Data is pulled periodically there is the scenario that between syncs, a user might delete a value in HealthKit. That delete needs to be propagated back to the server on the next sync.

My initial thought was to do this with an Observer Query.

My Question is: Using an Observer Query for HealthKit, is there a way to determine if the action that triggered the query is a delete action?

public void CheckForDelete (Subject subject)
    {
        var sampleType = HKObjectType.GetCorrelationType (HKCorrelationTypeKey.IdentifierFood);
        var predicate = HKQuery.GetPredicateForSamples (NSDate.DistantPast, NSDate.Now, HKQueryOptions.None);
        var observerQuery = new HKObserverQuery (sampleType, predicate, (query, completion, error ) => {
            //...Determine if action was a Delete
            //Code to delete on backend

            completion();
        });

        HealthKitStore.ExecuteQuery (observerQuery);
    }
jmf
  • 41
  • 2
  • did you find a solution for this yet? I'm facing the same issue and I think there's no easy way to track deleted samples. I think I need to check if the samples still exist in HealthApp and delete them manually. – Jorge May 18 '15 at 16:19
  • Unfortunately not yet. I tried to create a separate observer query for each element to listen for deletes and then trigger a database delete when that observer query is called but that didn't behave as i thought it would. It also created a background query thread for every element (which could be in the hundreds). As far as i can tell there really is no way to detect whether healthkit data was deleted from a separate app. Hopefully somebody figures it out though! – jmf May 19 '15 at 17:52
  • The way I did was saving the entries to my own database, and when the `observerQuery` hits I call a `sampleQuery` to get the actual samples. That done, I iterate thought the samples and check what was added and what was deleted. I created a column `healthUUID` in my database to save the identification of the `Health` samples, that way, it is easy to track differences between my database and `health app`. – Jorge May 20 '15 at 10:59
  • if you wish to do something similar, let me know and I can share the code. – Jorge May 20 '15 at 11:01
  • hey guys, have you discovered how to solve the problem? – GGirotto Jan 10 '18 at 23:31

1 Answers1

1

In iOS 9, HKAnchoredObjectQuery has been modified to report deleted objects.

Allan
  • 7,039
  • 1
  • 16
  • 26