Search code examples
iosswifthealthkithkhealthstorehksamplequery

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


I want to query samples from HealthKit but in order to prevent inaccurate or manipulated data I don't want samples that were written to health by other apps. Does anyone have any idea what predicate I can use to filter out data from all apps or to only allow data from devices? Thanks in advance.

Edit: I've realized that apps can save data to health with an HKDevice included. So filtering out samples that don't have devices won't work.


Solution

  • 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.