在iOS 13之前提取HealthKit锻炼的详细心率

时间:2019-12-04 21:35:58

标签: healthkit

我想访问最详细的运动心率列表(从5:26 PM到5:58 PM)。在iOS 13上,使用打印 385心率的代码听起来很容易:

if #available(iOS 13.0, *) {  
     var index = 1  
     let quantityType = HKQuantityType.quantityType(forIdentifier: .heartRate)!  
     let predicate = HKQuery.predicateForSamples(withStart: self.workout.startDate, end: self.workout.endDate, options: [.strictStartDate])  
     let heartRateUnit = HKUnit.count().unitDivided(by: HKUnit.minute())  
     let query2 = HKQuantitySeriesSampleQuery(quantityType: quantityType, predicate: predicate) {  
          (query, quantity, dateInterval, sample, done, error) in  

          let rate = quantity!.doubleValue(for: heartRateUnit)  
          print("#\(index) \(rate)")  
          index += 1          
      }  

     HealthKitManager.shared.healthStore.execute(query2)  
}  

但是在iOS 12及更低版本上,按照Followig代码,我只有 4个示例

  • 2019-02-06 4:26:32 PM +0000到2019-02-06 4:26:32 PM +0000:138.03914270987403
  • 2019-02-06 4:36:27 PM +0000到2019-02-06 4:36:27 PM +0000:168.30417943409182
  • 2019-02-06 4:46:33 PM +0000到2019-02-06 4:46:33 PM +0000:177.63391439737114
  • 2019-02-06 4:56:25 PM +0000到2019-02-06 4:56:25 PM +0000:167.2611138237847
let heartRateQuery = HKSampleQuery(  
     sampleType: HKQuantityType.quantityType(forIdentifier: .heartRate)!,  
     predicate: HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [.strictStartDate]),  
     limit: HKObjectQueryNoLimit,  
     sortDescriptors: [ NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: true) ]) { (_: HKSampleQuery, results: [HKSample]?, error: Error?) in  

     let heartRateUnit = HKUnit.count().unitDivided(by: HKUnit.minute())  

     for result in results! {  
          if let sample:HKQuantitySample = result as? HKQuantitySample {  
               let rate = sample.quantity.doubleValue(for: heartRateUnit)       
               print("\(sample.startDate) to \(sample.startDate) : \(rate)")            
          }  
     }  

如何使用iOS <13获得更多示例?我想念什么?

0 个答案:

没有答案