I need to read multiple Queries of HKStatisticsCollectionQuery Like StepCount and Swimming Stroke

This is the the func for Authorisation from health App

 func stepCount(completion: @escaping (Bool) -> Void){
 if HKHealthStore.isHealthDataAvailable(){
let stepCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!  let swimmingStrokeCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.swimmingStrokeCount)!
  guard let healthStore = self.healthStore else { return completion(false) }
   healthStore.requestAuthorization(toShare: [], read: [swimmingStrokeCount,stepCount]) { (success, error) in
 completion(success)
            }
  }
    }

and then Im not able to read both step count and swimming Stroke at the the same query

 func readData(completion: @escaping (HKStatisticsCollection?)-> Void){

        let stepCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!

        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)

        query = HKStatisticsCollectionQuery(quantityType: stepCount, quantitySamplePredicate: predicate,options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: daily)

        query1 = HKStatisticsCollectionQuery(quantityType: swimmmingStrokeCount, 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)

        }

    }

is there any possible way to call both query and query1 as a single query

also I have tried to call them entirely separate as two different queries but that too is not working as expected it results becomes sum of step count and swimming stroke

can anyone clear my doubt?

Answered by magwin_santhosh in 747162022

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")

                            }

                        }

                    }

                }

            }
Accepted Answer

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")

                            }

                        }

                    }

                }

            }
I need to read multiple Queries of HKStatisticsCollectionQuery Like StepCount and Swimming Stroke
 
 
Q