recoverActiveWorkoutSession() inaccurate for paused sessions

When a workout session is being recovered, if it is paused, the elapsed time will be incorrect. It will seem like that workout never was paused.

The recovery works fine if the workout was never paused.

Steps to reproduce:

  1. Implement recoverActiveWorkoutSession
  2. Start workout
  3. Pause session and print the elapsed time
  4. Stop simulator / cause crash
  5. When recoverActiveWorkoutSession is called the elapsed time will not equal the elapsed time when the session was paused.

Here is my implementation. I haven't seen any examples online.

guard let recovered = try? await healthStore.recoverActiveWorkoutSession() else {return}
        self.session = recovered
        self.builder = recovered.associatedWorkoutBuilder()
        self.session?.delegate = self
        self.builder?.delegate = self
        self.builder?.dataSource = HKLiveWorkoutDataSource(healthStore: healthStore, workoutConfiguration: recovered.workoutConfiguration)
        self.sessionState = recovered.state

This sounds like a HealthKit bug to me, and so I’d suggest that you file a feedback report for the HealthKit folks to take a look – If you do so, please share your report ID here.

Does the elapsed time continue to tick up after the workout session is recovered? If yes, you might be able to work around the issue by recording the pause time and adjusting the elapsed time with it. A pause event can be observed in workoutSession(_:didGenerate:).

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

recoverActiveWorkoutSession() inaccurate for paused sessions
 
 
Q