- iOS 8.0+
- watchOS 2.0+
The sample query returns sample objects that match the provided type and predicate. You can provide a sort order for the returned samples, or limit the number of samples returned. Other query classes can be used to perform more specialized searches and calculations. For more information, see
Sample queries are immutable: The query’s properties are set when the query is first created. They cannot change.
You create a sample query by calling the
init(sample initializer. After the query is instantiated, you run it by calling the HealthKit store’s
execute(_:) method. This method runs the query on an anonymous background queue. When the query is complete, it executes the results handler on the background queue. Typically, you dispatch these results back to the main queue to update your user interface.
Listing 1 shows how to build a query and process the results. This code is taken from the
AAPLJournal file in the sample code project Fit: Store and Retrieve HealthKit Data.
The above sample code starts by calculating the start and end dates for the desired time interval, to produce a 24-hour period, starting at the previous midnight. Next, it defines a sample type for dietary energy consumed, and a predicate that filters out any samples that don’t fall within the desired time interval.
With the sample type and predicate created, it builds the query. This query returns any samples measuring dietary energy consumed that fall within the time frame. There is no limit placed on the number of samples it returns, and the results are not sorted.
The results handler starts by checking to see whether an error occurred. If there is an error, it prints a log message to the console and aborts. Otherwise, it dispatches back to the main thread to update the UI.
The dispatch block begins by clearing the view controller’s array of food items. Next, it iterates over each result, grabbing the name of the food (from the metadata) and the amount of energy (in joules). It uses these values to create a new food item, which it adds to the food item array.
After all the results have been processed, the results handler asks the table view to load the new data.
After the results handler’s block is defined, the query is complete. This query is then executed using the HealthKit store.
Like many HealthKit classes, the
HKSample class should not be subclassed.