Chandan kumar
Chandan kumar

Reputation: 1084

Healthkit multiple HKAnchoredObjectQuery not returning data for all sample types

I am using HKAnchoredObjectQuery with updateHandler to get multiple types of healthkit sample data but it returns data only for "HKQuantityTypeIdentifierStepCount" not for other HKSampleTypes. Below is the code that I am using. Any help would be much appreciated.

func readHealthKitData(type: HKSampleType) {  
    
        let onAnchorQueryResults : ((HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, NSError?) -> Void)! = {
            (query:HKAnchoredObjectQuery, addedObjects:[HKSample]?, deletedObjects:[HKDeletedObject]?, newAnchor:HKQueryAnchor?, nsError:NSError?) -> Void in
            
            if (addedObjects?.count > 0)
            {
                
                // var addDict = [String : AnyObject]()
                for obj in addedObjects! {
                    let hkValue = obj as? HKQuantitySample
                    if(hkValue?.UUID.UUIDString != nil){
                        print("TypeName:",type.isEqual("HKQuantityTypeIdentifierHeartRate"))
                        switch  type {
                        case  HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!:
                            let val =  (hkValue?.quantity.doubleValueForUnit(HKUnit(fromString: "count/min")))!
                            let uuid : String = (hkValue?.UUID.UUIDString)!
                                                  
                        case HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!:
                            let val =  (hkValue?.quantity.doubleValueForUnit(HKUnit.countUnit()))!
                            let uuid : String = (hkValue?.UUID.UUIDString)!
                           
                            
                        case HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!:
                            let val =  ((hkValue?.quantity.doubleValueForUnit(HKUnit.gramUnit()))!/1000.0)
                            let uuid : String = (hkValue?.UUID.UUIDString)!

                        default:
                            break
                            
                        }
                        
                    }
                }
                
            }
            
        }
        
        let queryEndDate = NSDate()
        print("Before Start: ")
        print(self.queryStartDate)
        let predicate: NSPredicate = HKAnchoredObjectQuery.predicateForSamplesWithStartDate(self.queryStartDate, endDate: queryEndDate, options: HKQueryOptions.None)
        
        let anchoredQuery = HKAnchoredObjectQuery(type: type, predicate: predicate, anchor: hkAnchor, limit: 0, resultsHandler: onAnchorQueryResults)
        anchoredQuery.updateHandler = onAnchorQueryResults
        healthStore.executeQuery(anchoredQuery)
        self.queryStartDate = queryEndDate
        
        
    }}

Upvotes: 1

Views: 984

Answers (2)

Sajjad
Sajjad

Reputation: 61

You can use this init that is available for iOS 15.0 and higher.

init(queryDescriptors: [HKQueryDescriptor], 
anchor: HKQueryAnchor?, 
limit: Int, 
resultsHandler handler: @escaping (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void)

Upvotes: 1

Allan
Allan

Reputation: 7353

This is expected. The query's initializer takes a type parameter only returns results of that type. You cannot query for multiple types of samples with a single query.

Upvotes: 3

Related Questions