Exclude own samples from healthkit queries

497 views Asked by At

I'm writing an app that amongst other things, reads weight samples from HealthKit.
I'm also writing samples.
I'm trying to read the latest sample that isn't mine:

NSPredicate* non_fdct = [NSCompoundPredicate notPredicateWithSubpredicate:[HKQuery predicateForObjectsFromSource:[HKSource defaultSource]]];
NSSortDescriptor *last = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate ascending:NO];
HKSampleQuery* query = [[HKSampleQuery alloc] initWithSampleType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass] predicate:non_fdct limit:1 sortDescriptors:@[last] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {  ... };

But I'm getting my own samples if they are the latest samples.
Any idea?

1

There are 1 answers

1
Allan On

The way you have constructed the non_fdct predicate is not quite correct. Try this instead:

NSPredicate *non_fdct = [NSPredicate predicateFromString:@"%K != %@", HKPredicateKeyPathSource, [HKSource defaultSource]];