Reputation: 2040
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?
Upvotes: 0
Views: 526
Reputation: 7363
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.
Upvotes: 1