0

I am creating an iWatch app which reads HeartRate data which I then use to plot some graphs. There are 2 buttons in the app, one to pause the workout session and other to resume the workout session. On click of Pause button, I am calling pauseWorkoutSession method of healthStore:

healthStore.pauseWorkoutSession(workoutSession!)

OnClick of Resume Button, I am calling resumeWorkoutSession method of healthStore:

healthStore.resumeWorkoutSession(workoutSession!)

To get HeartRate data, I am running the following code:

let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
            let datePredicate = HKQuery.predicateForSamplesWithStartDate(instanceDel.workoutSessionStartDate, endDate: nil, options: .StrictStartDate)


            let updateHandler: (HKAnchoredObjectQuery,[HKSample]?,[HKDeletedObject]?,HKQueryAnchor?,
                NSError?)-> Void  = {query, samples, deleteObjects, newAnchor, error  in

                    if !self.instanceDel.workoutPaused {
                        //do something 
                    }
            }

heartRateQuery = HKAnchoredObjectQuery(type: quantityType, predicate: datePredicate, anchor: nil, limit: Int(HKObjectQueryNoLimit) , resultsHandler: updateHandler)            
heartRateQuery!.updateHandler = updateHandler
instanceDel.healthStore.executeQuery(heartRateQuery!)

The problem I am facing is that update handler is continuously called and new heart rate values are are continuously added to samples array even when workout is paused. What does pauseWorkoutSession exactly does? Isn' t it suppose to pause the workout session so that new new heart rate reading come?

Ketan P
  • 4,259
  • 3
  • 30
  • 36
Deep Arora
  • 1,900
  • 2
  • 24
  • 40

1 Answers1

1

Pausing a workout isn't intended to prevent heart rate samples or any other type of samples from being collected by watchOS. Think about it this way - even when the watch is not recording a workout, it is still collecting heart rate, steps, distance, calories, etc.

If you want to ignore samples that are recorded while your session is paused, you should stop the query or ignore the samples.

Allan
  • 7,039
  • 1
  • 16
  • 26
  • Do you know what exactly the PauseWorkoutSession method does? Why do I ever call this method? @Allan – Deep Arora Oct 01 '16 at 08:33
  • Pausing the workout generates a pause HKWorkoutEvent. The events, which are delivered to your HKWorkoutSessionDelegate, will be used when creating an HKWorkout to determine the total un-paused duration of the workout. – Allan Oct 01 '16 at 23:15
  • When workout activity type is running , Watch can auto detect when you are not running and pauses the workout, generating the HKWorkoutEvent which could be used to determine the total un-paused time. But, If I manually pause and resume the workout, I can very easily calculate the total un-paused time without the HKWorkoutEvent as I know the time when I pressed the pause and resume button. Is there any use of calling PauseWorkoutSession then – Deep Arora Oct 02 '16 at 05:49