HKQuery class is the basis for all the query objects that retrieve data from the HealthKit store. The
HKQuery class is an abstract class. You should never instantiate an
HKQuery object directly. Instead, you always work with one of its concrete subclasses:
- iOS 8.0+
- watchOS 2.0+
This query returns an anchor for the last sample saved to the HealthKit store. On subsequent queries, you can use the anchor to request only the samples that have been added after the anchor. This lets you quickly query for new items in the store. See
This query allows more complex filtering of complex, correlation data. With it, you provide a separate predicate for each of the sample types stored in the correlation. A correlation is returned only if the correlation’s predicate and all of its contents’ predicates match. See
These long-running queries call their update handler whenever a matching object is saved to the HeathKit store. With these queries, you can observe and respond to data saved in the background by other apps or entered directly into the store from connected Bluetooth devices. You can also register observer queries for background delivery. With background delivery, HealthKit will launch your app in response to changes in the store, letting your app respond to those changes. See
This is a general purpose query. Use sample queries to access any type of sample data. Sample queries are particularly useful when you want to sort the results or limit the total number of samples returned. See
This query returns a list of sources (apps and devices) that have saved data that matches a specified sample type and predicate. See
These queries calculate aggregate values (average, minimum, maximum, or sum) for quantity samples data. See
Like the statistics query, this query returns average, minimum, maximum or sum data for the specified quantity samples. However, instead of returning a single result, this query lets you specify a time interval and it performs the calculation repeatedly over that interval, returning an array of results.
For example, by specifying a day long interval and a sum calculation, you can calculate the number of steps the user has taken each day over a series of days. Each entry in the resulting array represents the sum of steps for one day. See
Queries and Predicates
All the concrete
HKQuery subclasses take a predicate. You can use this predicate to filter the samples returned by the query. When HealthKit runs a query, it converts the predicate to SQL and executes the SQL on the underlying store. This has two important side effects.
Predicates improve the performance of your query, both in terms of speed and memory usage. Because the predicate is performed by the store, it restricts the number of HealthKit objects that are instantiated and returned.
Since the predicates are executed by the store, you are limited in the type of predicates that you can use. Specifically, HealthKit provides a number of predicate key paths (for example,
Key Path UUID
HKPredicate). You can create predicates using only these key paths.
Key Path Metadata
Like many HealthKit classes, the
HKQuery class should not be subclassed.