Post

Replies

Boosts

Views

Activity

Reply to I need to read multiple Queries of HKStatisticsCollectionQuery Like StepCount and Swimming Stroke
I have solved the problem by creating the two separate func for two separate queries like   func stepCount(completion: @escaping (HKStatisticsCollection?)-> Void){         let stepCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!         let startDate = Calendar.current.date(byAdding: .day,value: -7, to: Date())         let anchorDate = Date.monday12AM()         let daily = DateComponents(day : 1)         let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictStartDate)         query = HKStatisticsCollectionQuery(quantityType: stepCount, quantitySamplePredicate: predicate,options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: daily)   query!.initialResultsHandler = { query, statisticsCollection, error in             completion(statisticsCollection)         }         if let healthStore = healthStore,let query = self.query{             healthStore.execute(query)         }     }     func swimmingStroke(completion : @escaping (HKStatisticsCollection?)-> Void){         let swimmmingStrokeCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.swimmingStrokeCount)!         let startDate = Calendar.current.date(byAdding: .day,value: -7, to: Date())         let anchorDate = Date.monday12AM()         let daily = DateComponents(day : 1)         let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictStartDate)         query1 = HKStatisticsCollectionQuery(quantityType: swimmmingStrokeCount, quantitySamplePredicate: predicate,options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: daily)         query1!.initialResultsHandler = { query1, statisticsCollection1, error in             completion(statisticsCollection1)         }         if let healthStore1 = healthStore,let query1 = self.query1{             healthStore1.execute(query1)         }     } and updating the UI by if let healthStore1 = healthStore {                 healthStore1.authorization { success in                     if success{                         healthStore1.swimmingStroke{ statisticsCollection1  in                             if let statisticsCollection1 = statisticsCollection1{                                 print(statisticsCollection1)                                 UpdateUIFromStatistics1(statisticsCollection1)                             }else{                                 print("no statistics Collection Data1")                             }                         }                     }                 }             }             if let healthStore = healthStore {                 healthStore.authorization { success in                     if success{                         healthStore.readData{ statisticsCollection  in                             if let statisticsCollection = statisticsCollection{                                 print(statisticsCollection)                                 UpdateUIFromStatistics(statisticsCollection)                             }else{                                 print("no statistics Collection Data")                             }                         }                     }                 }             }
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’23