Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Querying heart rate samples hangs for some users

This is an ongoing issue that I haven't been able to solve:

I am querying different types of HealthKit data over the past year. While this works fine for HRV, it hangs for some users when I'm trying to get heart rate data.

Here's the relevant query

func initialRead(from startDate: Date) async throws -> [HKSample] {
        let endDate = anchorStart
        let interval: TimeInterval = .days(7)
        var currentStartDate = startDate
        var currentEndDate = Date(timeInterval: interval, since: currentStartDate)
        var samples: [HKSample] = []
        while currentStartDate <= endDate {
            let datePredicate = SampleType.datePredicate(start: currentStartDate, end: currentEndDate)
            let predicate =   NSCompoundPredicate(andPredicateWithSubpredicates: [datePredicate,HKQuery.predicateForObjects(withMetadataKey: HKMetadataKeyHeartRateMotionContext, allowedValues: [HKHeartRateMotionContext.sedentary])])
            do {
                let result = try await withCheckedThrowingContinuation { continuation in
                    
                    let completionQuery = HKSampleQuery(sampleType: HKQuantityType.heartRate, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: [.init(key: HKSampleSortIdentifierStartDate, ascending: true)]) { query, samples, error in
                        if let samples {
                            continuation.resume(returning: samples)
                        } else {
                            if let error {
                                continuation.resume(throwing: error)
                            } else {
                                continuation.resume(returning: [])
                            }
                        }
                    }
                    healthStore.execute(completionQuery)
                }
                samples = samples.merge(from: result)
            } catch {
                Logger.general.error("Reading failed for dates \(currentStartDate) to \(currentEndDate): \(error)")
            }
            currentStartDate = currentEndDate
            currentEndDate = Date(timeInterval: interval, since: currentStartDate)
        }
        return samples
    }

extension HKSampleType {
    
    static func datePredicate( start:Date?, end:Date?) -> NSPredicate {
        HKQuery.predicateForSamples(withStart: start, end:end, options: .strictStartDate)
    }
}

For reference, I expect about 1000 sedentary samples per week. Basically what happens for these users is when they start reading the HR data, the app hangs. They start each read manually via a special TestFlight build with buttons for starting the different data type readings.

Any advice on how to proceed with this bug would be great since it only affects some users.

I am able to create test builds for this audience to test different options. One theory is the motion context predicate is screwing something up. If any apple dev can enlighten me how to narrow down the issue, that would be great.

Answered by visskiss in 829129022

Upon isolation of the issue, I don't believe it's a health kit reading issue that I'm encountering.

Accepted Answer

Upon isolation of the issue, I don't believe it's a health kit reading issue that I'm encountering.

Querying heart rate samples hangs for some users
 
 
Q