Sample CodeiOS, iPadOS, Mac CatalystReviewed 2026-07-21View on Apple Developer

Customizing workouts with WorkoutKit

At a glance

Item Summary
Purpose Create, preview, and sync workouts for use in the Workout app on Apple Watch.
App architecture A Swift sample with the source-visible chain SamplePlannerAppSamplePlannerViewWorkoutStoreWorkoutKit APIs.
Main patterns Central store
Project style 5 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: Task, await suspension point; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper.
Key frameworks/packages SwiftUI, WorkoutKit, HealthKit; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── Sample Planner/
│   ├── SamplePlannerApp.swift
│   ├── SamplePlannerView.swift
│   ├── WorkoutStore.swift
│   ├── PresentPreviewDemo.swift
│   └── HKWorkoutConfiguration+Displaying.swift
├── Configuration/
│   └── SampleCode.xcconfig
└── Sample Planner.xcodeproj/
    ├── .xcodesamplecode.plist
    └── project.pbxproj

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 3 project/configuration file(s) and 4 source declaration(s).

Overall architecture

Reference code

Sample Planner/SamplePlannerApp.swift:10 — architecture anchor

@main
struct SamplePlannerApp: App {
    var body: some Scene {
        WindowGroup {
            SamplePlannerView()
        }
    }
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into WorkoutKit.

Ownership and state

Ownership evidence

Sample Planner/SamplePlannerView.swift:12 — stored dependency or nearest verified ownership anchor

struct SamplePlannerView: View {
    @State var authorizationState: WorkoutScheduler.AuthorizationState = .notDetermined
    // ...
}
Owner Object or state Relationship Mutation authority
SamplePlannerView AuthorizationState (authorizationState) owns wrapper-managed state App/module collaborators
SamplePlannerView Array (scheduledWorkouts) owns wrapper-managed state App/module collaborators
SamplePlannerView RelativeDateTimeFormatter (dateFormatter) stores or receives Initialized by the owner; the binding is immutable
PresentPreviewDemo WorkoutPlan (cyclingWorkoutPlan) stores or receives Initialized by the owner; the binding is immutable

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

Concern Source mechanism Verified placement or handoff Evidence
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. Sample Planner/SamplePlannerView.swift:52
Suspension boundary await suspension point The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. Sample Planner/SamplePlannerView.swift:53

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

Reference code

Sample Planner/SamplePlannerView.swift:52 — representative execution boundary

                        Task {
                            authorizationState = await WorkoutScheduler.shared.requestAuthorization()
                            await update()
                        }

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. Sample Planner/PresentPreviewDemo.swift:13
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. Sample Planner/PresentPreviewDemo.swift:8
Source import WorkoutKit The cited file imports this module; runtime use and architectural role are not inferred. Sample Planner/PresentPreviewDemo.swift:9
Source import HealthKit The cited file imports this module; runtime use and architectural role are not inferred. Sample Planner/HKWorkoutConfiguration+Displaying.swift:8

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

Class and protocol design

Sample Planner/SamplePlannerApp.swift:11 — representative type boundary

@main
struct SamplePlannerApp: App {
    // ...
            SamplePlannerView()
    // ...
}
Type Responsibility Depends on or conforms to
SamplePlannerApp Application entry and top-level composition App
SamplePlannerView User-interface presentation and input forwarding View
WorkoutStore Centralized state or persistence access Concrete collaborators/imported frameworks
PresentPreviewDemo Represents a feature value or composable behavior View

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
cyclingWorkoutPlan (Sample Planner/PresentPreviewDemo.swift:12) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
schedule (Sample Planner/SamplePlannerView.swift:116) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.
update (Sample Planner/SamplePlannerView.swift:134) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.
scheduleMenuOptions (Sample Planner/SamplePlannerView.swift:142) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.

Reference code

Sample Planner/PresentPreviewDemo.swift:12 — representative boundary

struct PresentPreviewDemo: View {
    private let cyclingWorkoutPlan: WorkoutPlan
    // ...
}

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Application entry and top-level composition SamplePlannerApp The source’s App suffix makes this role explicit.
Centralized state or persistence access WorkoutStore The source’s Store suffix makes this role explicit.
User-interface presentation and input forwarding SamplePlannerView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Central store Sample Planner/WorkoutStore.swift:11 A store-named type centralizes feature state or persistence.

Naming conventions

  • Types: App: SamplePlannerApp; Store: WorkoutStore; View: SamplePlannerView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: schedule, update, scheduleMenuOptions, createCyclingCustomWorkout, cyclingBlockOne, cyclingBlockTwo, createGolfWorkout, createRunningCustomWorkout.
  • Files: Sample Planner/SamplePlannerApp.swift, Sample Planner/SamplePlannerView.swift, Sample Planner/WorkoutStore.swift, Sample Planner/PresentPreviewDemo.swift.

Architecture takeaways

  • SamplePlannerApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, WorkoutKit, HealthKit through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
Sample Planner/SamplePlannerApp.swift Cited implementation, SamplePlannerApp
Sample Planner/SamplePlannerView.swift Cited implementation, Task, await suspension point, SamplePlannerView, SamplePlannerView_Previews
Sample Planner/PresentPreviewDemo.swift Cited implementation, SwiftUI state property wrapper, SwiftUI, WorkoutKit, PresentPreviewDemo, PresentPreviewDemo_Previews
Sample Planner/WorkoutStore.swift WorkoutStore
Sample Planner/HKWorkoutConfiguration+Displaying.swift HealthKit, Feature implementation