healthkit restingHeartRate simulator

I try to get data of the restingHeartRate via HKStatisticsCollectionQuery. The simulator doesnt seems to have any records. I can retrieve active heart rate data, and I collected those via a HKWorkoutSession.

I set the authorization:

`  
let readDataTypes: Set = [
            HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!,
            HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.restingHeartRate)!,
            HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.biologicalSex)!,
            HKObjectType.characteristicType(forIdentifier: HKCharacteristicTypeIdentifier.dateOfBirth)!,
            HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!,
        ]`

The code will retrieve data if I change .restingHeartRate to .heartRate

    let calendar = Calendar.current
    let beatsPerMinuteUnit = HKUnit.count().unitDivided(by: HKUnit.minute())
    let restingHeartRateType = HKQuantityType.quantityType(forIdentifier: .restingHeartRate)! 
    let endDate = Date()
    let startDate = calendar.date(byAdding: .day, value: -14, to: endDate)!
    let restingPredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])
    let anchorDate = calendar.date(bySettingHour: 0, minute: 0, second: 0, of: Date())!
    var interval = DateComponents()
    interval.day = 1
    let restingHRquery = HKStatisticsCollectionQuery(quantityType: restingHeartRateType,
                                            quantitySamplePredicate: restingPredicate,
                                            options: .discreteAverage,
                                            anchorDate: anchorDate,
                                            intervalComponents: interval)
  restingHRquery.initialResultsHandler = {
    restingQuery, statisticsCollection, error in
    guard let statsCollection = statisticsCollection else { return}
    print ("statsCollection init \(statsCollection.statistics().count)")
  }
  
  restingHRquery.statisticsUpdateHandler =  {
    restingQuery, statistics, statisticsCollection, error in
    guard let statsCollection = statisticsCollection else { return }
    print ("statsCollection update\(statsCollection.statistics().count)")

Any advice on how to retrieve some test data?

  • I deployed the code via TestFlight, unfortunately I don't get real data even on a real device. .restingHeartRate

    the code works if I change to .heartRate

Add a Comment

Replies

...