如何将一系列 HKQuantity 样本(心率)保存到锻炼中
How to save an array of HKQuantitySamples (heart rate) to a workout
Apple 的文档示例代码通过 add
方法将平均心率 HKQuantitySample
保存到锻炼中,但是对于给定的锻炼,我试图保存锻炼期间获取的所有心率值,即[HKQuantitySample]
我该怎么做?下面是我的代码添加第一个值只是为了测试,但我想全部添加?
var heartRateValues = [HKQuantitySample]()
func processHeartRateSamples(_ samples: [HKQuantitySample]) {
for sample in samples {
heartRateValues.append(sample)
}
}
private func addSamples(toWorkout workout: HKWorkout, from startDate: Date, to endDate: Date) {
// Create energy and distance samples
let totalEnergyBurnedSample = HKQuantitySample(type: HKQuantityType.activeEnergyBurned(),
quantity: totalEnergyBurnedQuantity(),
start: startDate,
end: endDate)
let totalDistanceSample = HKQuantitySample(type: HKQuantityType.distanceWalkingRunning(),
quantity: totalDistanceQuantity(),
start: startDate,
end: endDate)
// Add samples to workout
healthStore.add([totalEnergyBurnedSample, totalDistanceSample, heartRateValues.first!], to: workout) { (success: Bool, error: Error?) in
guard success else {
print("Adding workout subsamples failed with error: \(String(describing: error))")
return
}
}
}
您已经有 heartRateValues
作为 [HKQuantitySample]
所以只需这样做:
private func addSamples(toWorkout workout: HKWorkout, from startDate: Date, to endDate: Date) {
// Create energy and distance samples
let totalEnergyBurnedSample = HKQuantitySample(type: HKQuantityType.activeEnergyBurned(),
quantity: totalEnergyBurnedQuantity(),
start: startDate,
end: endDate)
let totalDistanceSample = HKQuantitySample(type: HKQuantityType.distanceWalkingRunning(),
quantity: totalDistanceQuantity(),
start: startDate,
end: endDate)
let samples = [HKQuantitySample]()
samples.append(totalEnergyBurnedSample)
samples.append(totalDistanceSample)
samples.append(contentsOf: heartRateValues)
// Add samples to workout
healthStore.add(samples, to: workout) { (success: Bool, error: Error?) in
guard success else {
print("Adding workout subsamples failed with error: \(String(describing: error))")
return
}
}
}
基本上你创建一个样本数组添加totalEnergyBurnedSample
和totalDistanceSample
然后整个heartRateValues
数组然后在healthStore.add
方法中传递那个sample
参数...
Apple 的文档示例代码通过 add
方法将平均心率 HKQuantitySample
保存到锻炼中,但是对于给定的锻炼,我试图保存锻炼期间获取的所有心率值,即[HKQuantitySample]
我该怎么做?下面是我的代码添加第一个值只是为了测试,但我想全部添加?
var heartRateValues = [HKQuantitySample]()
func processHeartRateSamples(_ samples: [HKQuantitySample]) {
for sample in samples {
heartRateValues.append(sample)
}
}
private func addSamples(toWorkout workout: HKWorkout, from startDate: Date, to endDate: Date) {
// Create energy and distance samples
let totalEnergyBurnedSample = HKQuantitySample(type: HKQuantityType.activeEnergyBurned(),
quantity: totalEnergyBurnedQuantity(),
start: startDate,
end: endDate)
let totalDistanceSample = HKQuantitySample(type: HKQuantityType.distanceWalkingRunning(),
quantity: totalDistanceQuantity(),
start: startDate,
end: endDate)
// Add samples to workout
healthStore.add([totalEnergyBurnedSample, totalDistanceSample, heartRateValues.first!], to: workout) { (success: Bool, error: Error?) in
guard success else {
print("Adding workout subsamples failed with error: \(String(describing: error))")
return
}
}
}
您已经有 heartRateValues
作为 [HKQuantitySample]
所以只需这样做:
private func addSamples(toWorkout workout: HKWorkout, from startDate: Date, to endDate: Date) {
// Create energy and distance samples
let totalEnergyBurnedSample = HKQuantitySample(type: HKQuantityType.activeEnergyBurned(),
quantity: totalEnergyBurnedQuantity(),
start: startDate,
end: endDate)
let totalDistanceSample = HKQuantitySample(type: HKQuantityType.distanceWalkingRunning(),
quantity: totalDistanceQuantity(),
start: startDate,
end: endDate)
let samples = [HKQuantitySample]()
samples.append(totalEnergyBurnedSample)
samples.append(totalDistanceSample)
samples.append(contentsOf: heartRateValues)
// Add samples to workout
healthStore.add(samples, to: workout) { (success: Bool, error: Error?) in
guard success else {
print("Adding workout subsamples failed with error: \(String(describing: error))")
return
}
}
}
基本上你创建一个样本数组添加totalEnergyBurnedSample
和totalDistanceSample
然后整个heartRateValues
数组然后在healthStore.add
方法中传递那个sample
参数...