Queries

Query health and fitness data.

Overview

Use queries to read sample data from the HealthKit store. Queries can also be used to list all the sources for a particular type of data, or to do statistical calculations. For example, statistical queries can calculate the minimum and maximum heartrate for a given week, or the total step count for a given day.

You run a query by calling the HealthKit store’s executeQuery: method. A snapshot of the current results are asynchronously returned to the query’s results handler. Long-running queries continue to monitor the HealthKit store, and return any relevant changes to the query’s update handler. To return sorted or filtered results, give the query a sort descriptor or predicate.

Topics

First Steps

Reading Data from HealthKit

Use queries to request sample data from HealthKit.

Common Query Types

HKSampleQuery

A general-purpose query that returns a snapshot of all the matching samples currently saved in the HealthKit store.

HKQuantitySeriesSampleQuery

A query that accesses the series data associated with a quantity sample.

HKAnchoredObjectQuery

A query that returns only recent changes to the HealthKit store, including a snapshot of new changes and continuous monitoring as a long-running query.

HKObserverQuery

A long-running query that monitors the HealthKit store and updates your app whenever a matching sample is saved to or deleted from the HealthKit store.

HKCorrelationQuery

A query that performs complex searches based on the correlation’s contents, and returns a snapshot of all matching samples.

HKDocumentQuery

A query that returns a snapshot of all matching documents currently saved in the HealthKit store.

HKHeartbeatSeriesQuery

A query that returns the heartbeat data contained in a heartbeat series sample.

Beta
HKQuery

An abstract class for all the query classes in HealthKit.

Sources and Devices

HKSourceRevision

An object indicating the source of a HealthKit sample.

HKSource

An object indicating the app or device that created a HealthKit sample

HKDevice

A device that generates data for HealthKit.

HKSourceQuery

A query that returns a list of sources (apps and devices) that have saved matching queries to the HealthKit store.

Calculating Statistics

HKStatisticsQuery

A query that performs statistical calculations over a set of matching quantity samples, and returns the results.

HKStatisticsCollectionQuery

A query that performs multiple statistics queries over a series of fixed-length time intervals, and returns the results.

HKStatistics

An object that represents the result of calculating the minimum, maximum, average, or sum over a set of samples from the HealthKit store.

HKStatisticsCollection

An object that manages a collection of statistics; each statistics object represents the data calculated over a separate time interval.

HKStatisticsOptions

Options for specifying the statistic to calculate.

See Also

Health Data

Saving Data to HealthKit

Create and share HealthKit samples.

Reading Data from HealthKit

Use queries to request sample data from HealthKit.

Data Types

Specify the kind of data used in HealthKit.

Samples

Create and save health and fitness samples.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software