gpt4 book ai didi

ios - 如何将一组 HKQuantitySample(心率)保存到锻炼中

转载 作者:行者123 更新时间:2023-11-28 12:15:18 28 4
gpt4 key购买 nike

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
}


}
}

基本上,您创建一个样本数组添加 totalEnergyBurnedSampletotalDistanceSample然后整个 heartRateValues数组,然后传递 sample healthStore.add 中的参数方法...

关于ios - 如何将一组 HKQuantitySample(心率)保存到锻炼中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46835438/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com