1

How can I get the details of the workouts like start and end date time of a a particular workout.

I am getting all the workout done using the following , and my aim is to get the heart rate during the duration of the workout.

  var q = new HKSampleQuery(HKObjectType.GetWorkoutType(), p, 0, new NSSortDescriptor[] { },
            new HKSampleQueryResultsHandler((HKSampleQuery query2, HKSample[] results, NSError error2) =>
            {
               
             
            }));
Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
Ashish Kumar
  • 755
  • 2
  • 6
  • 25

1 Answers1

0

You can set specific type(HKQuantityType ) to query the heart rate .

HKQuantityType heart = HKQuantityType.Create(HKQuantityTypeIdentifier.HeartRate);

var q = new HKSampleQuery(heart, p, 0, new NSSortDescriptor[] { },
   new HKSampleQueryResultsHandler((HKSampleQuery query2, HKSample[] results, NSError error2) =>
   {


    }));

Refer to How to read heart rate from iOS HealthKit app using Swift? .

ColeX
  • 14,062
  • 5
  • 43
  • 240
  • I need to first fetch all the workouts in the given duration, which I already have. Now I want to fetch the heart rate during the workouts. So the query I have posted gives me the duration not the start and end time so I cant get the heart rate only of that specific period. – Ashish Kumar Jul 26 '21 at 09:11
  • You can specify the start and end time in `predicate ` parameter ,check the answer in that link i provided. – ColeX Jul 26 '21 at 09:19
  • With my query I pass the start and end date in predicate and the query returns the list of workouts , but it returns the duration along with workout names not the start and end time. I need the start and end time so that I can get heart rate say between 1 to 2 pm. – Ashish Kumar Jul 26 '21 at 09:57
  • It worked like charm, I am getting the heart rate as well. But I wanted to understand what is the difference between HKSampleQuery and HKStatisticsCollectionQuery while collecting the heart rate. I want to collect heart say in every 2 min interval during an exercise which I can do only using HKStatisticsCollectionQuery . But somehow HKStatisticsCollectionQuery is not returning values but when I add a heart rate manually in health kit it does return the values – Ashish Kumar Aug 05 '21 at 08:00
  • Check the docs : https://developer.apple.com/documentation/healthkit/hkstatisticscollectionquery?language=objc and https://developer.apple.com/documentation/healthkit/hksamplequery?language=objc , i think it is well explained . – ColeX Aug 05 '21 at 08:05