Removing a Water Sample from HealthKit

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:

enter image description here

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, :

, ?

+1
2

HealthKit . , , HealthKitStore.deleteObject(). , .

+1

, . , obj-c :

// get an instance of the health store
self.healthStore = [[HKHealthStore alloc] init];

// the interval of the samples to delete (i observed that for a specific value if start and end date are equal the query doesn't return any objects)
NSDate *startDate = [dateOfSampleToDelete dateByAddingTimeInterval:0];
NSDate *endDate = [dateOfSampleToDelete dateByAddingTimeInterval:1];

// the type you're trying to delete (this could be heart-beats/steps/water/calories/etc..)
HKQuantityType *waterType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater];

// the predicate used to execute the query
NSPredicate *queryPredicate = [HKSampleQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionNone];

// prepare the query
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:waterType predicate:queryPredicate limit:100 sortDescriptors:nil resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {

    if (error) {

        NSLog(@"Error: %@", error.description);

    } else {

        NSLog(@"Successfully retreived samples");

        // now that we retrieved the samples, we can delete it/them
        [self.healthStore deleteObject:[results firstObject] withCompletion:^(BOOL success, NSError * _Nullable error) {

            if (success) {

                NSLog(@"Successfully deleted entry from health kit");

            } else {

                NSLog(@"Error: %@", error.description);

            }
        }];

    }
}];

// last but not least, execute the query
[self.healthStore executeQuery:query];

, [results firstObject]

+1

All Articles