HealthKit SDK Not Responding When Querying Step Data on iPhone 16 Pro Max

We have working code to fetch step data from HealthKit after requesting the necessary permissions. However, we’ve encountered an issue specific to one device, the iPhone 16 Pro Max. When querying the data, we do not receive a response, and the code enters an infinite loading state without completing the request.

The user who is facing this issue has tried logging in on another device, and it works fine. On the problematic device (iPhone 16 Pro Max), the request does not complete.

For reference, I’ve included the code below. Resolving this issue is crucial, so we would appreciate any guidance on what steps we can take to troubleshoot or resolve the problem on this specific device.

Please note that the device has granted permission to access HealthKit data.

static let healthStore = HKHealthStore()

static func limitReadFromHealthKitBetweenDates(fromDate: Date, toDate: Date = Date(), completion: @escaping ([HKStatistics]) -> Void) {
    guard let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount) else { return }

    let ignoreUserEntered = HKQuery.predicateForObjects(withMetadataKey: HKMetadataKeyWasUserEntered, operatorType: .notEqualTo, value: true)
    let now = toDate
    var interval = DateComponents()
    interval.day = 1

    var calendar = Calendar.current
    calendar.locale = Locale(identifier: "en_US_POSIX")

    var anchorComponents = calendar.dateComponents([.day, .month, .year], from: now)
    anchorComponents.hour = 0
    let anchorDate = calendar.date(from: anchorComponents) ?? Date()

    let query = HKStatisticsCollectionQuery(quantityType: stepsQuantityType,
                                            quantitySamplePredicate: ignoreUserEntered,
                                            options: [.cumulativeSum],
                                            anchorDate: anchorDate,
                                            intervalComponents: interval)

    query.initialResultsHandler = { _, results, error in
        guard let results = results else {
            print("Error returned from resultHandler: \(String(describing: error?.localizedDescription))")
            return
        }
        
        print(results)
        
        var statisticsArray: [HKStatistics] = []
        results.enumerateStatistics(from: fromDate, to: now) { statistics, _ in
            statisticsArray.append(statistics)
            
            if statistics.endDate.getddmmyyyyslashGMT == now.getddmmyyyyslashGMT {
                completion(statisticsArray)
            }
        }
    }

    healthStore.execute(query)
}

Please note that the code works on all devices except the problematic one. Could you please guide me on the next steps to resolve this issue?

When querying the data, we do not receive a response, and the code enters an infinite loading state without completing the request.

Assuming that you meant your initialResultsHandler was never triggered after your query was executed (healthStore.execute(query)), I believe that's a HealthKit bug because, in any case, HKStatisticsCollectionQuery should call the initial results handler after a period of time; if there is an error, it should call the handler with the error.

I’d hence suggest that you file a feedback report with a sysdiagnose and share your report ID here for folks to track. For the instructions of how to capture a sysdiagnose, see here.

I don't see anything that can work around the issue, but for debugging purpose, I am guessing that the issue may be related to the dataset on the device's HealthKit store. You might check if the following is relevant:

  • The anchor date components. I see that you set anchorComponents.hour to 0. I'd expect that the other components, such as anchorComponents.minute and anchorComponents.second, are set to a reasonable value as well. I know that the components may have default values, but depending the calendar and locale, the values may or may not be valid.

  • The predicate. You might consider giving your predicate a start date and end date. Changing the predicate changes the dataset HKStatisticsCollectionQuery needs to process, and may be able to help HKStatisticsCollectionQuery work around the issue it has.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

HealthKit SDK Not Responding When Querying Step Data on iPhone 16 Pro Max
 
 
Q