My application currently allows users to save WaterIntakeRecord, and then they are saved using Core Data. I can write in HealthKit, keeping the water withdrawal record as HKQuantitySample.
I add WaterIntakeRecord12.9 ounces of liquid to my application. Since in Healthcare I have milliliters as my preferred unit of measurement, it shows in milliliters:

Where am I afraid when I try to remove a sample.
When the user saves WaterIntakeRecord, he can select the unit of measure in which they want to save the sample, and then convert this measurement to ounces of the USA, which is a property WaterIntakeRecord. Thus, each WaterIntakeRecordhas an agreed unit of measurement, which can be converted to other units of measure (all found in Health) for display.
When I try to delete a saved sample, I try:
static func deleteWaterIntakeSampleWithAmount(amount: Double, date: NSDate) {
guard let type = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryWater) else {
print("———> Could not retrieve quantity type for identifier")
return
}
let quantity = HKQuantity(unit: HKUnit.fluidOunceUSUnit(), doubleValue: amount)
let sample = HKQuantitySample(type: type, quantity: quantity, startDate: date, endDate: date)
HealthKitStore.deleteObject(sample) { (success, error) -> Void in
if let err = error {
print("———> Could not delete water intake sample from HealthKit: \(err)")
} else {
print("———> Deleted water intake sample from HealthKit")
}
}
}
When a user deletes an entry in my application, she must delete the corresponding template in HealthKit. The record was successfully deleted from my application, however, I continue to receive an error while trying to remove the sample from HealthKit using the method above:
Error Domain=com.apple.healthkit Code=100 "Transaction failure." UserInfo {NSLocalizedDescription=Transaction failure.}
I am not sure what I am doing wrong to get this error.
amount - WaterIntakeRecord ouncesUS, date - WaterIntakeRecord date, :
, ?