Cannot convert value of type 'Set<HKQuantityType>' to expected argument type 'HKQuantityType

below is my code of reading data from HealthKit for Multiple Data

but for reading Multiple data using Set() it shows

"Cannot convert value of type 'Set' to expected argument type 'HKQuantityType' "

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

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

        let MoveCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.appleMoveTime)!

        let DistanceCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)!

        let Activitysummary = Set([stepCount,MoveCount,DistanceCount])

        

        let startDate = Calendar.current.date(byAdding: .day, value: -7, to: Date())

        //MARK: here we setting our start time at 12.00 AM

        let anchorDate = Date.monday12AM()

        //MARK: we going to calculate the value for each day

        let daily = DateComponents(day : 1)

        let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictStartDate)

        //MARK: .cummulativesum -> iPhone and Apple Watch Difference

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

//MARK:  here in ActivitySummary it show the error

        

        query!.initialResultsHandler = { query,statisticsCollection, error in

            completion(statisticsCollection)

        }

        if let healthStore = healthStore, let query = self.query{

            healthStore.execute(query)

        }

    }

Accepted Reply

I'm not sure it is possible to have query with several identifiers.

So you could try to have a query for each stepCount, moveCount, distanceCount and compute statistics yourself for the 3.

See here: https://stackoverflow.com/questions/71273378/how-to-handle-multiple-queries-of-different-hkquantitytypeidentifier

can only query only one HKQuantityIdentifier at a time

Add a Comment

Replies

I tested the following (in playgrounds) and got no error:

        let stepCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
        let MoveCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.appleMoveTime)!
        let DistanceCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)!
        let Activitysummary = Set([stepCount,MoveCount,DistanceCount])

So, where exactly do you get the error ?

Note: var names should start with lowercase: distanceCount, activitySummary…

yeah till this there is no problem the problem arises on below code

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

here in quantityType: Activitysummary throws an error saying "Cannot convert value of type 'Set' to expected argument type 'HKQuantityType "

I'm not sure it is possible to have query with several identifiers.

So you could try to have a query for each stepCount, moveCount, distanceCount and compute statistics yourself for the 3.

See here: https://stackoverflow.com/questions/71273378/how-to-handle-multiple-queries-of-different-hkquantitytypeidentifier

can only query only one HKQuantityIdentifier at a time

Add a Comment