HealthKit data - Is HKStatisticsCollectionQuery slow?

Hi,

I am using HealthKit for the first time. I am using HKStatisticsCollectionQuery. I am running my code iOS 17 on a physical iPhone.

It takes several seconds to query data, for example 1 day worth of heart rate at 1 minute resolution. I changed the resolution to 1 hour, expecting it to be faster, but it's pretty much the same…

I have been following the official documentation and sample code.

I also compiled in Release, but that didn't really help for HKStatisticsCollectionQuery.

I quickly looked with Instruments, the app is spending a lot of time in decoding data with NSXPCDecoder.

Is there a way to speed data retrieval? Or this is "expected" latency?

Several seconds for one day heart rate statistics doesn't sound crazy, but whether it makes sense really depends on how many samples you have (and so HKStatisticsCollectionQuery needs to calculate). If you don't mind to share the following information, I'd be able to take a closer look:

  1. The code snippet you used to set up your HKStatisticsCollectionQuery.
  2. The way you used to observe the query time.
  3. The number of heart rate samples you have on the day.

I am also curious if the relatively long query time leads to a real issue in your app, given that HealthKit runs queries in its own background queue.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I have this issue too, it takes a good 20 odd seconds for the queries to come back on WatchOS.

I am making several queries at once though, but even then it shouldn't be taking 20s on my Apple Watch, whereas it only takes around 1s on iPhone.

I am using iOS 18.0.1, and WatchOS 11.0.1, both physical devices.

HealthKit data - Is HKStatisticsCollectionQuery slow?
 
 
Q