Read Workout Effort Scores

Is there documentation on how to read workout effort scores from a HealthKit workout? I'm interested in reading workoutEffortScore and estimatedWorkoutEffortScore. I have not been successful trying to read them using the same method that I do other workout HKQuantityTypes (heartRate, stepCount, etc). I'm using Swift and I do have authorization for those types requested and granted.

I have found documentation on setting these values (https://developer.apple.com/forums/thread/763539) but not reading them.

Thank You

Answered by DTS Engineer in 820948022

I haven't checked .estimatedWorkoutEffortScore yet, but for .workoutEffortScore, if I related the sample correctly, as described in the mentioned post, the following code returns the right sample to me:

// let predicate = HKQuery.predicateForObjects(from: workout)
let predicate = HKQuery.predicateForSamples(withStart: workout.startDate, end: workout.endDate)
let query = HKSampleQuery(sampleType: HKQuantityType(.workoutEffortScore),
                          predicate: predicate,
                          limit: HKObjectQueryNoLimit,
                          sortDescriptors: []) { (_, results, error) in
    print("\(String(describing: results?.first))")
}
healthStore.execute(query)

Using HKQuery.predicateForObjects(from: workout) as the predicate for the query doesn't return anything to me, which does surprise me, given that the sample is correctly related to the workout. I’d suggest that you file a feedback report against that – If you do so, please share your report ID here.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

I haven't checked .estimatedWorkoutEffortScore yet, but for .workoutEffortScore, if I related the sample correctly, as described in the mentioned post, the following code returns the right sample to me:

// let predicate = HKQuery.predicateForObjects(from: workout)
let predicate = HKQuery.predicateForSamples(withStart: workout.startDate, end: workout.endDate)
let query = HKSampleQuery(sampleType: HKQuantityType(.workoutEffortScore),
                          predicate: predicate,
                          limit: HKObjectQueryNoLimit,
                          sortDescriptors: []) { (_, results, error) in
    print("\(String(describing: results?.first))")
}
healthStore.execute(query)

Using HKQuery.predicateForObjects(from: workout) as the predicate for the query doesn't return anything to me, which does surprise me, given that the sample is correctly related to the workout. I’d suggest that you file a feedback report against that – If you do so, please share your report ID here.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thank you for that information, that is exactly what I needed. I had only been trying to create the predicate using the HKQuery.predicateForObjects(from: workout), which does not work.

The way you wrote works, and it looks like there is a new call that also works: HKQuery.predicateForWorkoutEffortSamplesRelated(workout: workout, activity: nil)

Given this new workoutEffort specific call works with a workout for an input I'm guessing that it not working with the predicateForObjects is not a bug but as designed.

Thanks again, I really appreciate it @DTS Engineer !

Read Workout Effort Scores
 
 
Q