我想访问最详细的运动心率列表(从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个示例:
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获得更多示例?我想念什么?