I'm trying to grab data from the Health App. Specifically data that the user did not enter in themselves. For instance, I have an iPhone 6+ that logs the amount of steps that I take. There is also an option to add the data manually; If you add the data manually, the health app marks the data as "user added".
Here's what's confusing me. Let's say I added a step count of 22. When I query the data using HKStatisticsQuery with a predicate of
HKQuery.predicateForObjectsWithMetadataKey(HKMetadataKeyWasUserEntered, allowedValues: [true])
I get the correct result of 22 steps, since I set the allowedValues to true and that I added this myself. However, when I try to set allowedValues to false, I get no results
HKQuery.predicateForObjectsWithMetadataKey(HKMetadataKeyWasUserEntered, allowedValues: [false])
I do indeed have the step data in the health app, but it returned no results.
Check for the below possible areas to fix it:
nil
.authorizationStatusForType:
method available with HKHealthStore class.Update 1:
My observations on wasUserEntered key is:
HKQuantitySample
stores metadata
dictionary along with HKWasUserEntered
key as TRUE automatically.metadata
dictionary with key HKWasUserEntered
along with value as either TRUE/FALSE. Otherwise, the metadata
property will contain nil
object. Hence, Apple is not applying predicate(predicate contains metadata key) on the data which don't have metadata with it.For debugging this metadata, try to print your HKQuantitySampleObject.metadata
Apple's implementation on metadata Vs NSPredicate:
metadata
dictionary to the respective health record.metadata
dictionary for his record of health data.metadata
for a specific health record and NSPredicate
have a constraint on metadata
then, HealthKit completely omitting to validate such records.Finally,
It is advised to use
instead of
+ (instancetype)quantitySampleWithType:(HKQuantityType *)quantityType
quantity:(HKQuantity *)quantity
startDate:(NSDate *)startDate
endDate:(NSDate *)endDate;
to add metadata.
metadata
key) should be applied on all the data irrespective of checking for metadata
exists or not.