iOS 9 delete data in HealthKit

578 Views Asked by At

While I am trying to delete some records I previously saved in HealthKit, I got an error if I use -deleteObjectsOfType:predicate:withCompletion:. The error says:

Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.healthd.server" UserInfo={NSDebugDescription=connection to service named com.apple.healthd.server}

But if I first query the records using the same type and predicate, and then delete the returned samples with -deleteObjects:withCompletion:. It works.

Here's the type and predicate if it helps:

HKCategoryType *type = [HKCategoryType categoryTypeForIdentifier:HKCategoryTypeIdentifierMenstrualFlow];
NSPredicate *predicate = [HKQuery predicateForObjectsFromSource:[HKSource defaultSource]];

Anyone had the same issue? Thanks.

1

There are 1 best solutions below

0
On

This is a known issue with deleteObjectsOfType:predicate:withCompletion: when using some predicates (including the one you've specified). The workaround you describe is appropriate. And as I mentioned in the comments, you should file a bug with Apple.

However, note that the specifying the predicate [HKQuery predicateForObjectsFromSource:[HKSource defaultSource]] when deleting objects is unnecessary. Your app can only delete its own samples, so it is redundant to provide a predicate that matches samples from the default source.