Class

HKAnchoredObjectQuery

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.

Overview

Anchored object queries are mostly immutable. You can assign the query’s updateHandler 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.

guard let sampleType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) else {
    fatalError("Unable to create a step count sample type")
}
 
let query = HKAnchoredObjectQuery(type: sampleType,
                                  predicate: nil,
                                  anchor: self.anchor,
                                  limit: Int(HKObjectQueryNoLimit)) { [unowned self](query, newSamples, deletedSamples, newAnchor, error) -> Void in
                                    
                                    guard let samples = newSamples as? [HKQuantitySample], let deleted = deletedSamples else {
                                        // Add proper error handling here...
                                        print("*** Unable to query for step counts: \(error?.localizedDescription) ***")
                                        abort()
                                    }
                                    
                                    // Process the results...
                                    self.anchor = newAnchor
                                    
                                    for sample in samples {
                                        self.addStepCountSample(sample)
                                    }
                                    
                                    for deletedSample in deleted {
                                        self.removeStepCountSample(deletedSample)
                                    }
                                    
                                    print("Done!")
}
healthStore.executeQuery(query)

The code sample performs the following steps:

  1. It creates a step count sample type.

  2. 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.

  3. It defines a result handler for the query.

    1. The result handler checks for errors.

    2. If no errors have occurred, the result handler saves the new anchor object and processes the new sample and deleted object data.

  4. 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.

Relationships

Inherits From

Conforms To