Skip to content
AF
HealthKit Queries

HealthKit Queries

HealthKit has a query class for each shape of answer: HKSampleQuery for the samples themselves, HKStatisticsQuery and HKStatisticsCollectionQuery for totals and averages, HKAnchoredObjectQuery for what changed since last time, HKObserverQuery for being told that something changed, plus dedicated classes for workout routes and activity rings. Each page here covers one class: what Apple documents it does, the initializer and handlers, a short Swift sketch built from documented calls, and the traps that return a wrong answer without an error. Every claim comes from Apple’s own HealthKit documentation, and where Apple is silent the page says so.

These pages explain how each query behaves. Setting HealthKit up (the capability, Info.plist keys, entitlements and authorization) is covered in the HealthKit integration guide. Specific failures, such as background delivery not firing or a read that returns nothing, have their own troubleshooting pages. The data types themselves are listed in the HealthKit identifier reference. Porting to Android? The HealthKit to Health Connect API equivalents map each class here to its Health Connect call.

Reading what is stored right now

A one-time snapshot of matching samples, and the predicate that decides what matches. Most HealthKit code starts here.

Numbers instead of samples

Let the store compute the sum, average or extreme, once for a window or once per day, instead of adding up samples yourself.

Keeping up with changes

What changed since last time, including deletions, and how HealthKit tells you something changed while your app is in the background.

Swift concurrency

The async/await descriptors that wrap the classes above and type their results.

Special-purpose reads

The GPS points behind a workout, and the daily activity rings.

Frequently asked questions

Which HealthKit query class should I use to read data?
It depends on the shape of the answer you want. For the samples themselves, once, use HKSampleQuery. For one total or average over a window, use HKStatisticsQuery, and for one value per day or hour, HKStatisticsCollectionQuery. To read only what changed since your last read, including deletions, use HKAnchoredObjectQuery, and to be told that something changed, HKObserverQuery. Workout routes need HKWorkoutRouteQuery and activity rings need HKActivitySummaryQuery. On iOS 15.4 and later, every one of these except HKObserverQuery also has an async/await query descriptor.
What is the difference between HKStatisticsQuery and HKStatisticsCollectionQuery?
The number of results. Apple's article on statistics collection queries puts it in one line: HKStatisticsQuery calculates a single value over all matching samples, while HKStatisticsCollectionQuery partitions the samples into time intervals and calculates a value for each interval. Both take the same HKStatisticsOptions and both work on quantity samples only. The collection query also needs an anchor date and interval components, and can keep running to deliver updates.
Which HealthKit queries keep running after they return their first results?
HKObserverQuery always does. HKAnchoredObjectQuery, HKStatisticsCollectionQuery and HKActivitySummaryQuery do when you assign their update handler before executing them; without one, Apple documents that they stop once the initial results are delivered. A long-running query continues until you pass it to the health store's stop(_:) method, so a screen that starts one should stop it when it goes away. HKSampleQuery and HKStatisticsQuery always finish after one result.

All 40 questions in HealthKit Queries, answered

Independent comparison, last reviewed October 3, 2026. Pricing, rate limits, and feature availability change often — confirm current details in each provider’s official documentation before you commit. Product and company names are trademarks of their respective owners; AIFitnessAPI is not affiliated with, endorsed by, or sponsored by any product listed here.