Anchored object queries provide an easy way to search for new data in the HealthKit store. An HKAnchoredObjectQuery object returns an anchor value that corresponds to the last sample or deleted object received by that query. Subsequent queries can use this anchor to restrict their results to only samples saved or deleted after the anchor’s sample.
- iOS 8.0+
- watchOS 2.0+
Anchored object queries are mostly immutable. You can assign the query’s
update property after instantiating the object. All other properties must be set when you instantiate the object, and they cannot change.
Executing Source Queries
The sample code, shown below, builds and executes an anchored object query.
The code sample performs the following steps:
It creates a step count sample type.
It uses the sample type to create an anchored object query. Because this example is querying for all step count samples, it does not use a predicate or limit to filter the results.
It defines a result handler for the query.
The result handler checks for errors.
If no errors have occurred, the result handler saves the new anchor object and processes the new sample and deleted object data.
It executes the query using the HealthKit store.
The first time this code runs, the
anchor property is set to
nil. The query returns all the step count data currently available in the HealthKit store. On subsequent runs, the code uses the anchor returned by the previous query. These queries return only the step count samples that have been saved or deleted since the previous run.