Im building a workout app to track swimming workouts for watchos 11. Triggering .prepare() on my HKWorkoutSession does not change the session state HKWorkoutSessionState.
Below is my prepare function which should transition the session state to HKWorkoutSessionStatePrepared. Nothing is thrown in the delegates, the state just wont change? I have tried erasing, restarting, use another version of xcode and another simulator runtime.
func prepare() {
guard self.session == nil else {
fatalError("Session already exist")
}
// Configure Workout Type
let config = HKWorkoutConfiguration()
config.activityType = .swimming
config.swimmingLocationType = .openWater
config.locationType = .outdoor
self.Workoutconfig = config
// Create Session
do {
guard store.authorizationStatus(for: .workoutType()) == .sharingAuthorized else {
fatalError("Lack of permission to start workout")
}
let session = try HKWorkoutSession(healthStore: store, configuration: config)
self.session = session
self.builder = session.associatedWorkoutBuilder()
Logger.diveController.info("Successfully created workout session")
builder?.dataSource = HKLiveWorkoutDataSource(healthStore: store, workoutConfiguration: config)
self.session?.delegate = self
self.builder?.delegate = self
if self.session == nil {
fatalError("No workout session created")
}
if self.builder == nil {
fatalError("No workout builder created")
}
self.session?.prepare()
logger.debug("Session Started at: \(self.session?.startDate ?? Date())")
logger.debug("Session State: \(self.session?.state.description ?? "")")
if self.session?.state != .prepared {
reset()
fatalError("Failed To Prepare")
}
} catch {
Logger.diveController.error("Error starting workout session: \(error.localizedDescription)")
}
}