Class

HKQuery

An abstract class for all the query classes in HealthKit.

Declaration

@interface HKQuery : NSObject

Overview

The 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 it directly. Instead, you always work with one of its concrete subclasses.

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, HKPredicateKeyPathUUID and HKPredicateKeyPathMetadata). You can create predicates using only these key paths.

Topics

Getting and Setting Properties

predicate

A predicate used to filter the objects returned from the HealthKit store.

sampleType

The type of objects being queried.

Deprecated
objectType

The type of objects being queried.

Creating Object Predicates

+ predicateForObjectWithUUID:

Returns a predicate that matches an object with the specified universally unique identifier (UUID).

+ predicateForObjectsWithUUIDs:

Returns a predicate that matches the objects with the specified universally unique identifiers (UUIDs).

+ predicateForObjectsFromSource:

Returns a predicate that matches all the objects that were created by the provided source.

+ predicateForObjectsFromSources:

Returns a predicate that matches all the objects that were created by any of the provided sources.

+ predicateForObjectsFromDevices:

Returns a predicate that matches all the objects that were created by any of the provided devices.

+ predicateForObjectsWithDeviceProperty:allowedValues:

Returns a predicate that matches all objects created by devices with the specified properties.

+ predicateForObjectsFromSourceRevisions:

Returns a predicate that matches all the objects that were created by any of the provided source revisions.

+ predicateForObjectsWithMetadataKey:

Returns a predicate that matches any object whose metadata contains the provided key.

+ predicateForObjectsWithMetadataKey:allowedValues:

Returns a predicate that matches objects based on the provided metadata key and an array of target values.

+ predicateForObjectsWithMetadataKey:operatorType:value:

Returns a predicate that matches objects based on the provided metadata key, value, and operator.

+ predicateForObjectsWithNoCorrelation

Returns a predicate that matches all objects that are not associated with a HealthKit correlation.

Creating Sample Predicates

+ predicateForSamplesWithStartDate:endDate:options:

Returns a predicate for samples whose start and end dates fall within the specified time interval.

HKQueryOptions

Constants that describe how a sample’s time period overlaps with the target time period.

Creating Quantity Sample Predicates

+ predicateForQuantitySamplesWithOperatorType:quantity:

Returns a predicate that matches samples based on the target quantity.

Creating Category Sample Predicates

+ predicateForCategorySamplesWithOperatorType:value:

Returns a predicate that checks a category sample’s value.

Creating Clinical Record Predicates

+ predicateForClinicalRecordsFromSource:FHIRResourceType:identifier:

Returns a predicate for a specific FHIR resource.

+ predicateForClinicalRecordsWithFHIRResourceType:

Returns a predicate for a specific FHIR type.

Creating Workout Predicates

+ predicateForObjectsFromWorkout:

Returns a predicate that matches any objects that have been associated with the provided workout.

+ predicateForWorkoutsWithOperatorType:duration:

Returns a predicate for matching workouts based on their duration.

+ predicateForWorkoutsWithOperatorType:totalDistance:

Returns a predicate for matching workouts based on the total distance traveled.

+ predicateForWorkoutsWithOperatorType:totalEnergyBurned:

Returns a predicate for matching workouts based on the total energy burned.

+ predicateForWorkoutsWithOperatorType:totalFlightsClimbed:

Returns a predicate that matches workout samples based on the number of flights climbed.

+ predicateForWorkoutsWithOperatorType:totalSwimmingStrokeCount:

Returns a predicate that matches workout samples based on the number of strokes while swimming.

+ predicateForWorkoutsWithWorkoutActivityType:

Returns a predicate for matching workouts based on the type of activity.

Creating Activity Summary Predicates

+ predicateForActivitySummaryWithDateComponents:

Returns a predicate that matches the activity summary for the specified day.

+ predicateForActivitySummariesBetweenStartDateComponents:endDateComponents:

Returns a predicate for matching all the activity summaries that fall between the days identified by the start and end date components.

Creating Predicate Format Strings

Use these keys when creating a predicate format string.

Relationships

Inherits From

See Also

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