How to force a HKQuery to load the most recent steps counts?

HealthKit does not always have an up-to-date count of the user’s steps and distance travelled. It imports these values, which actually come from CoreMotion.framework, periodically and in response to certain events. If a running application has an open HKObserverQuery or HKStatisticsCollectionQuery then HealthKit will stream the values to the client but otherwise the samples are just a snapshot from the last import.

So if you’d like to observe changes for a sample type, you should subscribe to updates using an HKObserverQuery and then query HealthKit again for the latest values. A more efficient approach would be to use HKStatisticsCollectionQuery, though, which has an update handler that will be invoked as the statistics for the samples matching the predicates change.

Finally, if you’re only interested up-to-date step counts or distance travelled for at most the past 7 days then I recommend that you consider using CoreMotion.framework directly instead.

Leave a Comment