<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKQuery",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "c:objc(cs)HKQuery"
  },
  "title" : "HKQuery"
}
-->

# HKQuery

An abstract class for all the query classes in HealthKit.

```
class HKQuery
```

## Overview

The [`HKQuery`](/documentation/HealthKit/HKQuery) class is the basis for all the query objects that retrieve data from the HealthKit store. The [`HKQuery`](/documentation/HealthKit/HKQuery) class is an abstract class. You should never instantiate it directly. Instead, you always work with one of its concrete subclasses.

### Filter queries using 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 store executes the predicate, it restricts the number of HealthKit objects that it instantiates and returns.
- Since the store executes these predicates, it limits the type of predicates that you can use. Specifically, HealthKit provides several predicate key paths (for example, [`HKPredicateKeyPathUUID`](/documentation/HealthKit/HKPredicateKeyPathUUID) and [`HKPredicateKeyPathMetadata`](/documentation/HealthKit/HKPredicateKeyPathMetadata)). You can create predicates using only these key paths.

## Topics

### Accessing properties

[`predicate`](/documentation/HealthKit/HKQuery/predicate)

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

[`objectType`](/documentation/HealthKit/HKQuery/objectType)

The type of objects being queried.

[`sampleType`](/documentation/HealthKit/HKQuery/sampleType)

The type of objects being queried.

### Creating object predicates

[`+  predicateForObjectWithUUID:`](/documentation/HealthKit/HKQuery/predicateForObject(with:))

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

[`+  predicateForObjectsWithUUIDs:`](/documentation/HealthKit/HKQuery/predicateForObjects(with:))

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

[`+  predicateForObjectsFromSource:`](/documentation/HealthKit/HKQuery/predicateForObjects(from:)-7j3p2)

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

[`+  predicateForObjectsFromSources:`](/documentation/HealthKit/HKQuery/predicateForObjects(from:)-89b4t)

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

[`+  predicateForObjectsFromDevices:`](/documentation/HealthKit/HKQuery/predicateForObjects(from:)-9h87f)

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

[`+  predicateForObjectsWithDeviceProperty:allowedValues:`](/documentation/HealthKit/HKQuery/predicateForObjects(withDeviceProperty:allowedValues:))

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

[`+  predicateForObjectsFromSourceRevisions:`](/documentation/HealthKit/HKQuery/predicateForObjects(from:)-1ar4g)

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

[`+  predicateForObjectsWithMetadataKey:`](/documentation/HealthKit/HKQuery/predicateForObjects(withMetadataKey:))

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

[`+  predicateForObjectsWithMetadataKey:allowedValues:`](/documentation/HealthKit/HKQuery/predicateForObjects(withMetadataKey:allowedValues:))

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

[`+  predicateForObjectsWithMetadataKey:operatorType:value:`](/documentation/HealthKit/HKQuery/predicateForObjects(withMetadataKey:operatorType:value:))

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

[`+  predicateForObjectsWithNoCorrelation`](/documentation/HealthKit/HKQuery/predicateForObjectsWithNoCorrelation())

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

### Creating sample predicates

[`+  predicateForSamplesWithStartDate:endDate:options:`](/documentation/HealthKit/HKQuery/predicateForSamples(withStart:end:options:))

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

[`HKQueryOptions`](/documentation/HealthKit/HKQueryOptions)

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

### Creating quantity sample predicates

[`+  predicateForQuantitySamplesWithOperatorType:quantity:`](/documentation/HealthKit/HKQuery/predicateForQuantitySamples(with:quantity:))

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

### Creating category sample predicates

[`+  predicateForCategorySamplesWithOperatorType:value:`](/documentation/HealthKit/HKQuery/predicateForCategorySamples(with:value:))

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

[`HKCategoryValuePredicateProviding`](/documentation/HealthKit/HKCategoryValuePredicateProviding)

A protocol for objects that produce predicates that match category value samples.

[`+  predicateForCategorySamplesEqualToValues:`](/documentation/HealthKit/HKQuery/predicateForCategorySamplesEqualToValues:)

A predicate that returns category samples with a matching value.

### Creating clinical record predicates

[`+  predicateForClinicalRecordsFromSource:FHIRResourceType:identifier:`](/documentation/HealthKit/HKQuery/predicateForClinicalRecords(from:fhirResourceType:identifier:))

Returns a predicate for a specific FHIR resource.

[`+  predicateForClinicalRecordsWithFHIRResourceType:`](/documentation/HealthKit/HKQuery/predicateForClinicalRecords(withFHIRResourceType:))

Returns a predicate for a specific FHIR type.

[`+  predicateForVerifiableClinicalRecordsWithRelevantDateWithinDateInterval:`](/documentation/HealthKit/HKQuery/predicateForVerifiableClinicalRecords(withRelevantDateWithin:))

Returns a predicate that finds verifiable health records with a relevant date within the specified range.

### Creating workout predicates

[`+  predicateForObjectsFromWorkout:`](/documentation/HealthKit/HKQuery/predicateForObjects(from:)-5irg9)

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

[`+  predicateForWorkoutsWithWorkoutActivityType:`](/documentation/HealthKit/HKQuery/predicateForWorkouts(with:))

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

[`+  predicateForWorkoutsWithActivityPredicate:`](/documentation/HealthKit/HKQuery/predicateForWorkouts(activityPredicate:))

Returns a predicate for matching workouts based on the associated workout activities.

[`+  predicateForWorkoutsWithOperatorType:duration:`](/documentation/HealthKit/HKQuery/predicateForWorkouts(with:duration:))

Returns a predicate for matching workouts based on their duration.

[`+  predicateForWorkoutsWithOperatorType:quantityType:averageQuantity:`](/documentation/HealthKit/HKQuery/predicateForWorkouts(operatorType:quantityType:averageQuantity:))

Returns a predicate for matching workouts based the average value of an associated quantity type.

[`+  predicateForWorkoutsWithOperatorType:quantityType:maximumQuantity:`](/documentation/HealthKit/HKQuery/predicateForWorkouts(operatorType:quantityType:maximumQuantity:))

Returns a predicate for matching workout activities based the maximum value of an associated quantity type.

[`+  predicateForWorkoutsWithOperatorType:quantityType:minimumQuantity:`](/documentation/HealthKit/HKQuery/predicateForWorkouts(operatorType:quantityType:minimumQuantity:))

Returns a predicate for matching workout activities based the minimum value of an associated quantity type.

[`+  predicateForWorkoutsWithOperatorType:quantityType:sumQuantity:`](/documentation/HealthKit/HKQuery/predicateForWorkouts(operatorType:quantityType:sumQuantity:))

Returns a predicate for matching workout activities based the sum of an associated quantity type.

[`+  predicateForWorkoutsWithOperatorType:totalDistance:`](/documentation/HealthKit/HKQuery/predicateForWorkouts(with:totalDistance:))

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

[`+  predicateForWorkoutsWithOperatorType:totalEnergyBurned:`](/documentation/HealthKit/HKQuery/predicateForWorkouts(with:totalEnergyBurned:))

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

[`+  predicateForWorkoutsWithOperatorType:totalFlightsClimbed:`](/documentation/HealthKit/HKQuery/predicateForWorkouts(with:totalFlightsClimbed:))

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

[`+  predicateForWorkoutsWithOperatorType:totalSwimmingStrokeCount:`](/documentation/HealthKit/HKQuery/predicateForWorkouts(with:totalSwimmingStrokeCount:))

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

### Creating workout activity predicates

[`+  predicateForWorkoutActivitiesWithWorkoutActivityType:`](/documentation/HealthKit/HKQuery/predicateForWorkoutActivities(workoutActivityType:))

Returns a predicate for workout activities based on the type of activity performed.

[`+  predicateForWorkoutActivitiesWithOperatorType:duration:`](/documentation/HealthKit/HKQuery/predicateForWorkoutActivities(operatorType:duration:))

Returns a predicate for matching workout activities based on their duration.

[`+  predicateForWorkoutActivitiesWithStartDate:endDate:options:`](/documentation/HealthKit/HKQuery/predicateForWorkoutActivities(start:end:options:))

Returns a predicate for workout activities that occur between the start and end date.

[`+  predicateForWorkoutActivitiesWithOperatorType:quantityType:averageQuantity:`](/documentation/HealthKit/HKQuery/predicateForWorkoutActivities(operatorType:quantityType:averageQuantity:))

Returns a predicate for matching workout activities based the average value of an associated quantity type.

[`+  predicateForWorkoutActivitiesWithOperatorType:quantityType:maximumQuantity:`](/documentation/HealthKit/HKQuery/predicateForWorkoutActivities(operatorType:quantityType:maximumQuantity:))

Returns a predicate for matching workout activities based the maximum value of an associated quantity type.

[`+  predicateForWorkoutActivitiesWithOperatorType:quantityType:minimumQuantity:`](/documentation/HealthKit/HKQuery/predicateForWorkoutActivities(operatorType:quantityType:minimumQuantity:))

Returns a predicate for matching workout activities based the minimum value of an associated quantity type.

[`+  predicateForWorkoutActivitiesWithOperatorType:quantityType:sumQuantity:`](/documentation/HealthKit/HKQuery/predicateForWorkoutActivities(operatorType:quantityType:sumQuantity:))

Returns a predicate for matching workout activities based the sum of an associated quantity type.

### Creating activity summary predicates

[`+  predicateForActivitySummaryWithDateComponents:`](/documentation/HealthKit/HKQuery/predicateForActivitySummary(with:))

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

[`+  predicateForActivitySummariesBetweenStartDateComponents:endDateComponents:`](/documentation/HealthKit/HKQuery/predicate(forActivitySummariesBetweenStart:end:))

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

### Creating electrocardiogram predicates

[`+  predicateForElectrocardiogramsWithClassification:`](/documentation/HealthKit/HKQuery/predicateForElectrocardiograms(classification:))

Returns a predicate that matches electrocardiogram samples with the specified classification.

[`+  predicateForElectrocardiogramsWithSymptomsStatus:`](/documentation/HealthKit/HKQuery/predicateForElectrocardiograms(symptomsStatus:))

Returns a predicate that matches electrocardiogram samples with the specified symptom status.

[`+  predicateForObjectsAssociatedWithElectrocardiogram:`](/documentation/HealthKit/HKQuery/predicateForObjectsAssociated(electrocardiogram:))

Returns a predicate that matches symptom samples associated with the specified electrocardiogram.

### Creating predicate format strings

Use these keys when creating a predicate format string.

  <doc://com.apple.healthkit/documentation/HealthKit/predicate-format-strings>

### Creating sort descriptors

  <doc://com.apple.healthkit/documentation/HealthKit/healthkit-sort-descriptors>

## See Also

[`HKActivitySummaryQuery`](/documentation/HealthKit/HKActivitySummaryQuery)

A query for reading activity summary objects from the HealthKit store.

[`HKAnchoredObjectQuery`](/documentation/HealthKit/HKAnchoredObjectQuery)

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

[`HKDocumentQuery`](/documentation/HealthKit/HKDocumentQuery)

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

[`HKObserverQuery`](/documentation/HealthKit/HKObserverQuery)

A long-running query that monitors the HealthKit store and updates your app when the HealthKit store saves or deletes a matching sample.

[`HKSourceQuery`](/documentation/HealthKit/HKSourceQuery)

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

[`HKStatisticsQuery`](/documentation/HealthKit/HKStatisticsQuery)

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

[`HKStatisticsCollectionQuery`](/documentation/HealthKit/HKStatisticsCollectionQuery)

A query that performs multiple statistics queries over a series of fixed-length time intervals.

[`HKWorkoutRouteQuery`](/documentation/HealthKit/HKWorkoutRouteQuery)

A query to access the location data stored in a workout route.

## Relationships

### Conforms To

[`Hashable`](/documentation/Swift/Hashable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Sendable`](/documentation/Swift/Sendable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Equatable`](/documentation/Swift/Equatable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

### Inherited By

[`HKSampleQuery`](/documentation/HealthKit/HKSampleQuery)

[`HKUserAnnotatedMedicationQuery`](/documentation/HealthKit/HKUserAnnotatedMedicationQuery)

[`HKHeartbeatSeriesQuery`](/documentation/HealthKit/HKHeartbeatSeriesQuery)

[`HKElectrocardiogramQuery`](/documentation/HealthKit/HKElectrocardiogramQuery)

[`HKDocumentQuery`](/documentation/HealthKit/HKDocumentQuery)

[`HKSourceQuery`](/documentation/HealthKit/HKSourceQuery)

[`HKVerifiableClinicalRecordQuery`](/documentation/HealthKit/HKVerifiableClinicalRecordQuery)

[`HKStatisticsQuery`](/documentation/HealthKit/HKStatisticsQuery)

[`HKWorkoutRouteQuery`](/documentation/HealthKit/HKWorkoutRouteQuery)

[`HKCorrelationQuery`](/documentation/HealthKit/HKCorrelationQuery)

[`HKAnchoredObjectQuery`](/documentation/HealthKit/HKAnchoredObjectQuery)

[`HKQuantitySeriesSampleQuery`](/documentation/HealthKit/HKQuantitySeriesSampleQuery)

[`HKWorkoutEffortRelationshipQuery`](/documentation/HealthKit/HKWorkoutEffortRelationshipQuery)

[`HKStatisticsCollectionQuery`](/documentation/HealthKit/HKStatisticsCollectionQuery)

[`HKObserverQuery`](/documentation/HealthKit/HKObserverQuery)

[`HKActivitySummaryQuery`](/documentation/HealthKit/HKActivitySummaryQuery)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)