HealthKit Queries
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.
- HKSampleQuery: a one-time snapshot of matching samplesHow HKSampleQuery works per Apple's docs: the initializer, HKObjectQueryNoLimit, sort identifiers that only work inside queries, and the background queue.UpdatedVerified October 3, 2026
- HealthKit query predicates: predicateForSamples, HKQueryOptions and HKSamplePredicateWhat HKQuery.predicateForSamples(withStart:end:options:) matches by default, how strictStartDate and strictEndDate change it, and the other predicates.UpdatedVerified October 3, 2026
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.
- HKStatisticsQuery: one sum, average or extreme over a windowHKStatisticsQuery per Apple's docs: cumulative vs discrete options, the accessor each option needs, separateBySource, and why sumQuantity() comes back nil.UpdatedVerified October 3, 2026
- HKStatisticsCollectionQuery: daily totals, anchorDate and intervalComponentsHow HKStatisticsCollectionQuery buckets samples by anchorDate and intervalComponents, why statistics() skips empty days, and when the query keeps running.UpdatedVerified October 3, 2026
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.
- HKAnchoredObjectQuery: new samples, deleted objects and the HKQueryAnchorHow HKAnchoredObjectQuery returns only what changed: the nil first anchor, HKQueryAnchor, expiring HKDeletedObject records, and updateHandler.UpdatedVerified October 3, 2026
- HKObserverQuery and enableBackgroundDelivery: how the pair worksHKObserverQuery per Apple's docs: an update handler that only says data changed, the completion handler contract, and how background delivery wakes you.UpdatedVerified October 3, 2026
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.
- HKWorkoutRouteQuery: reading a workout's GPS route in batchesReading a HealthKit workout route: find the HKWorkoutRoute with an anchored query, then read CLLocation batches with HKWorkoutRouteQuery until done.UpdatedVerified October 3, 2026
- HKActivitySummaryQuery: reading Move, Exercise and Stand ringsHow HKActivitySummaryQuery reads daily ring data: date-component predicates that need a calendar, activityMoveMode, goal properties and the update handler.UpdatedVerified October 3, 2026
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.
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.