Healthkit HKWorkoutSession state not transitioning

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)")
        }
    }

Checking session?.state immediately after calling session?.prepare() most likely won't get the right result. Instead, you might detect the state change by implementing the workoutSession(_:didChangeTo:from📅 ) method in your workout session delegate (HKWorkoutSessionDelegate), as shown below:

    func workoutSession(_ workoutSession: HKWorkoutSession,
                        didChangeTo toState: HKWorkoutSessionState,
                        from fromState: HKWorkoutSessionState,
                        date: Date) {
        print("Session state changed from \(fromState.rawValue) to \(toState.rawValue)")
    }

After you call session?.prepare(), the above method should be triggered and prints the following log:

Session state changed from 1 to 5

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Healthkit HKWorkoutSession state not transitioning
 
 
Q