Ignore Manual Entries from Apple Health App as Data Source

Ignore manual entries from Apple Health app as Data Source

Samples in HealthKit that were manually entered by the user will have have a YES value for the HKMetadataKeyWasUserEntered metadata key. To create a predicate that matches only samples that were not user-entered, you could do use the following:

[NSPredicate predicateWithFormat:@"metadata.%K != YES", HKMetadataKeyWasUserEntered];

Note that this must be formulated as value != YES because the value for the key could be YES, NO, or nil and nil implies NO.

Detect if HealthKit activity has been entered manually

Apple only provides two properties for the HKSource class, the bundleIdentifier and the name of the source, as of iOS8.x

The bundle identifier of the entry if made manually will be com.apple.Health, which is the bundle identifier of the Health app. Notice the capital H. When you pull your data just ignore the data which has a bundle identifier of com.apple.Health.

That way you will be only considering activities which are not manual.

Hope this helps. Let me know if you need more information.

You can also refer the link here for another way to do this: Ignore manual entries from Apple Health app as Data Source

Is it possible to detect the resource of the step count on HealthKit on iOS?

let pred = NSPredicate(format: "metadata.%K != YES", HKMetadataKeyWasUserEntered)

Applying above predicate should help.

In case of objective - C

[NSPredicate predicateWithFormat:@"metadata.%K != YES", HKMetadataKeyWasUserEntered];

Filter HealthKit Queries to exclude entries made by your own app

Currently, only the "=" and "IN" operators are supported for predicates filtering HKSamples by source.

How to query for samples from health kit that have an HKDevice

I'm still open to suggestions and alternate solutions but here is my work-around since I was unable to figure out how to use a predicate to get the job done.

 let datePredicate = HKQuery.predicateForSamples(withStart:Date(), end: nil, options: [])

let sampleQuery = HKAnchoredObjectQuery(type: sampleType,
predicate: predicate,
anchor: nil,
limit: Int(HKObjectQueryNoLimit)) { query,
samples,
deletedObjects,
anchor,
error in
if let error = error {
print("Error performing sample query: \(error.localizedDescription)")
return
}

guard let samples = samples as? [HKQuantitySample] else { return }

// this line filters out all samples that do not have a device
let samplesFromDevices = samples.filter {
$0.device != nil && $0.sourceRevision.source.bundleIdentifier.hasPrefix("com.apple.health")
}
doStuffWithMySamples(samplesFromDevices)
}

As you can see, I just filter the data once it comes through rather than doing it before-hand.

Edit: Seems like the sources listed in health are separated into apps and actual devices. Not 100% sure how they do this but it seems like the sources under the device section all have a bundle identifier prefixed with com.apple.health. Hopefully this works.

Getting only AutoDetected activities from healthkit

I solved the problem on my own. This is how I done it.

func todaySteps(completion: (Double, NSError?) -> () )
{
let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)

let date = NSDate()
print(date)
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let newDate = cal.startOfDayForDate(date)
print(newDate)
let predicate = HKQuery.predicateForSamplesWithStartDate(newDate, endDate: NSDate(), options: .None)

let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: 0, sortDescriptors: nil) { query, results, error in
var steps: Double = 0
if results?.count > 0
{
for result in results as! [HKQuantitySample]
{
print("Steps \(result.quantity.doubleValueForUnit(HKUnit.countUnit()))")

// checking and truncating manually added steps
if result.metadata != nil {
// Theses steps were entered manually
}
else{
// adding steps to get total steps of the day
steps += result.quantity.doubleValueForUnit(HKUnit.countUnit())
}

}
}
completion(steps, error)
}
executeQuery(query)
}


Related Topics



Leave a reply



Submit