HealthKit:删除提供的锻炼的相关样本问题
HealthKit : Issue with associated samples deletion of provided workout
使用 HealthKit,我正在保存以下数据:
- 锻炼
- 活性能量
- 距离
我正在使用以下代码删除锻炼:
self.healthStore?.delete(workout, withCompletion: { (status, error) in
但上面的代码只是从 HealthKit 应用程序中删除了一项锻炼。我想删除锻炼及其相关样本。我该怎么做?
要删除关联样本,我们需要对特定 HKQuantityTypeIdentifier
.
执行删除查询
要从锻炼中删除活跃能量,请参考以下代码:
let energyBurnedQuantity = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)
let predicate = HKQuery.predicateForObjects(from: workout)
let energyQuery = HKSampleQuery(sampleType: energyBurnedQuantity!, predicate: predicate, limit: 100, sortDescriptors: nil) { (query, result, error) in
if error == nil {
guard let resultData = result else {
return
}
if resultData.count > 0 {
self.healthStore?.delete(resultData, withCompletion: { [unowned self] (status, error) in
if status == true {
print("Successfully deleted Energy.")
})
} else {
print("Error \(String(describing: error?.localizedDescription))")
}
})
}
}
}
self.healthStore?.execute(energyQuery)
使用 HealthKit,我正在保存以下数据:
- 锻炼
- 活性能量
- 距离
我正在使用以下代码删除锻炼:
self.healthStore?.delete(workout, withCompletion: { (status, error) in
但上面的代码只是从 HealthKit 应用程序中删除了一项锻炼。我想删除锻炼及其相关样本。我该怎么做?
要删除关联样本,我们需要对特定 HKQuantityTypeIdentifier
.
要从锻炼中删除活跃能量,请参考以下代码:
let energyBurnedQuantity = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)
let predicate = HKQuery.predicateForObjects(from: workout)
let energyQuery = HKSampleQuery(sampleType: energyBurnedQuantity!, predicate: predicate, limit: 100, sortDescriptors: nil) { (query, result, error) in
if error == nil {
guard let resultData = result else {
return
}
if resultData.count > 0 {
self.healthStore?.delete(resultData, withCompletion: { [unowned self] (status, error) in
if status == true {
print("Successfully deleted Energy.")
})
} else {
print("Error \(String(describing: error?.localizedDescription))")
}
})
}
}
}
self.healthStore?.execute(energyQuery)