Skip to content

Workout Data

This guide adapts the DocC 02-read flow into a practical host-app pattern.

Read session state in saveSession(state:)

swift
override func saveSession(state: SaveWorkoutState) {
    super.saveSession(state: state)

    // 1. Map WorkoutKit state to your backend payload.
    // 2. Persist asynchronously.
    // 3. Keep a publisher/future for post-workout completion.
}

It is important to call super.saveSession(state:) to ensure default WorkoutKit behavior remains active.

Coordinate persistence and post-workout completion

swift
override func displayPostWorkout() -> AnyPublisher<Void, Never> {
    guard let savePublisher else { return super.displayPostWorkout() }
    return savePublisher
}

Returning your async save publisher ensures the SDK waits for your data pipeline before fully finishing the workout.

Handle early exits

swift
override func quitWorkout() {
    // Optional: track user cancellation or clean up local state.
    super.quitWorkout()
}

Always call super.quitWorkout() so WorkoutKit can complete its internal teardown correctly.

Forward data to your backend

In production, teams typically normalize the completion state into their own event schema, then persist workout metrics and progression fields required by product analytics. You should perform this write before dismissing the workout flow.

Tracking and logging

For additional instrumentation, use the tracking and logging interfaces described in Tracking.