How to filter “active” HeartRate from HealthKit iO

2019-08-20 14:25发布

I am trying to fetch Heart Rate Data from HealthKit iOS by putting filter using HKQuery.predicateForObjects By using the following code:

let predicate = HKQuery.predicateForObjects(withMetadataKey: HKMetadataKeyHeartRateMotionContext, allowedValues: ["active"])


        var interval = DateComponents()
        interval.day = 1
        let query = HKStatisticsCollectionQuery(quantityType: type, quantitySamplePredicate: predicate, options: [.discreteAverage], anchorDate: startDate, intervalComponents:interval)
        query.initialResultsHandler = { query, results, error in
            if error != nil {

                return
            }

I tried different combinations for putting values in a different way

let predicate = HKQuery.predicateForObjects(withMetadataKey: HKMetadataKeyHeartRateMotionContext, allowedValues: ["1"])

OR

let predicate = HKQuery.predicateForObjects(withMetadataKey: HKMetadataKeyHeartRateMotionContext, allowedValues: ["active"])

OR

let predicate = HKQuery.predicateForObjects(withMetadataKey: HKMetadataKeyHeartRateMotionContext, allowedValues: [HKHeartRateMotionContext.sedantary])

I am not getting if am doing anything wrong somewhere in my code.

Please correct me.

Thanks in advance enter image description here

0条回答
登录 后发表回答