Building a multidevice workout app
At a glance
| Item | Summary |
|---|---|
| Purpose | Mirror a workout from a watchOS app to its companion iOS app, and perform bidirectional communication between them. |
| App architecture | A Swift sample bundle with entry-bearing project variants MirroringWorkoutsSample Watch App, MirroringWorkoutsSample, each leading to HealthKit / HealthKitUI APIs. |
| Main patterns | Delegate or data-source callbacks, SwiftUI environment injection, Binding-based state propagation, Publisher-backed observable state |
| Project style | 21 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: DispatchQueue.main.async, Task, await suspension point, @MainActor, Task closure isolated to MainActor; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, ObservableObject, @Published. |
| Key frameworks/packages | SwiftUI, HealthKit, os, Foundation, Charts; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── MirroringWorkoutsSample Watch App/
│ ├── MirroringWorkoutsSampleApp.swift
│ ├── PagingView.swift
│ ├── SummaryView.swift
│ ├── ActivityRingsView.swift
│ ├── AppDelegate.swift
│ ├── ControlsView.swift
│ └── MetricsView.swift
├── MirroringWorkoutsSample/
│ ├── MirroringWorkoutsSampleApp.swift
│ ├── ChartView.swift
│ └── SummaryView.swift
└── Shared/
├── WorkoutManager.swift
└── ElapsedTimeView.swift
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 6 project/configuration file(s) and 26 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["MirroringWorkoutsSample Watch App"]
V2["MirroringWorkoutsSample"]
Boundary["HealthKit / HealthKitUI APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
MirroringWorkoutsSample Watch App/MirroringWorkoutsSampleApp.swift:10 — architecture anchor
@main
struct MirroringWorkoutsSampleWatchApp: App {
@WKApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
private let workoutManager = WorkoutManager.shared
@SceneBuilder var body: some Scene {
WindowGroup {
PagingView()
.environmentObject(workoutManager)
}
}
}Interpretation
The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.
Ownership and state
classDiagram
SessionSateChange o-- HKWorkoutSessionState : newState
SessionSateChange *-- Date : date
SessionSateChange o-- HKWorkoutSessionState : sessionState
SessionSateChange *-- Double : heartRate
Ownership evidence
Shared/WorkoutManager.swift:15 — stored dependency or nearest verified ownership anchor
struct SessionSateChange {
let newState: HKWorkoutSessionState
let date: Date
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
SessionSateChange |
HKWorkoutSessionState (newState) |
stores or receives | Initialized by the owner; the binding is immutable |
SessionSateChange |
Date (date) |
owns value state | Initialized by the owner; the binding is immutable |
SessionSateChange |
HKWorkoutSessionState (sessionState) |
stores or receives | App/module collaborators |
SessionSateChange |
Double (heartRate) |
owns value state | App/module collaborators |
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 |
|---|---|---|---|
| Queue scheduling | DispatchQueue.main.async |
The source addresses the main dispatch queue. | MirroringWorkoutsSample Watch App/ActivityRingsView.swift:23 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | MirroringWorkoutsSample Watch App/AppDelegate.swift:16 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | MirroringWorkoutsSample Watch App/AppDelegate.swift:19 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | Shared/WorkoutManager+iOS.swift:27 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | Shared/WorkoutManager+iOS.swift:27 |
@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
MirroringWorkoutsSample Watch App/ActivityRingsView.swift:23 — representative execution boundary
DispatchQueue.main.async {
activityRingsObject.setActivitySummary(summaries?.first, animated: true)
}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. | MirroringWorkoutsSample Watch App/ActivityRingsView.swift:13 |
| State propagation | ObservableObject |
ObservableObject supplies an observation contract. | Shared/WorkoutManager.swift:13 |
| State propagation | @Published |
A published property can emit owner-controlled changes. | Shared/WorkoutManager.swift:21 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | MirroringWorkoutsSample Watch App/ActivityRingsView.swift:10 |
| Source import | HealthKit |
The cited file imports this module; runtime use and architectural role are not inferred. | MirroringWorkoutsSample Watch App/ActivityRingsView.swift:9 |
| Source import | os |
The cited file imports this module; runtime use and architectural role are not inferred. | MirroringWorkoutsSample Watch App/AppDelegate.swift:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | MirroringWorkoutsSample Watch App/ActivityRingsView.swift:8 |
| Source import | Charts |
The cited file imports this module; runtime use and architectural role are not inferred. | MirroringWorkoutsSample/ChartView.swift:10 |
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
MirroringWorkoutsSample Watch App/MirroringWorkoutsSampleApp.swift:11 — representative type boundary
@main
struct MirroringWorkoutsSampleWatchApp: App {
@WKApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
MirroringWorkoutsSampleWatchApp |
Application entry and top-level composition | App |
MirroringWorkoutsSampleApp |
Application entry and top-level composition | App |
WorkoutManager |
Long-lived feature or framework coordination | NSObject, ObservableObject |
PagingView |
User-interface presentation and input forwarding | View |
SummaryView |
User-interface presentation and input forwarding | View |
SummaryMetricView |
User-interface presentation and input forwarding | View |
ChartView |
User-interface presentation and input forwarding | View |
SummaryView |
User-interface presentation and input forwarding | View |
GridItemView |
User-interface presentation and input forwarding | View |
ElapsedTimeView |
User-interface presentation and input forwarding | 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 |
|---|---|---|---|
calendar (MirroringWorkoutsSample Watch App/ActivityRingsView.swift:13) |
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. |
startWorkout (MirroringWorkoutsSample Watch App/ControlsView.swift:45) |
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. |
appDelegate (MirroringWorkoutsSample Watch App/MirroringWorkoutsSampleApp.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. |
workoutManager (MirroringWorkoutsSample Watch App/MirroringWorkoutsSampleApp.swift:13) |
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. |
Reference code
MirroringWorkoutsSample Watch App/ActivityRingsView.swift:13 — representative boundary
struct ActivityRingsView: WKInterfaceObjectRepresentable {
@Environment(\.calendar) private var calendar
// ...
}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 | MirroringWorkoutsSampleApp, MirroringWorkoutsSampleWatchApp |
The source’s App suffix makes this role explicit. |
| Receives callback-driven events | AppDelegate |
The source’s Delegate suffix makes this role explicit. |
| Long-lived feature or framework coordination | WorkoutManager |
The source’s Manager suffix makes this role explicit. |
| User-interface presentation and input forwarding | ActivityRingsView, ChartView, ControlsView, ElapsedTimeView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Delegate or data-source callbacks | MirroringWorkoutsSample Watch App/AppDelegate.swift:13 |
Callback protocols invert event delivery back into the sample’s owner. |
| SwiftUI environment injection | MirroringWorkoutsSample Watch App/ControlsView.swift:13 |
The environment supplies state or a capability without threading it through every initializer. |
| Binding-based state propagation | MirroringWorkoutsSample/ChartView.swift:22 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
| Publisher-backed observable state | Shared/WorkoutManager.swift:21 |
Published properties notify observers while mutation remains with the state object. |
Naming conventions
- Types: App: MirroringWorkoutsSampleApp, MirroringWorkoutsSampleWatchApp; Delegate: AppDelegate; Manager: WorkoutManager; View: ActivityRingsView, ChartView, ControlsView, ElapsedTimeView, GridItemView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
consumeSessionStateChange,resetWorkout,sendData,updateForStatistics,workoutSession,displayMetricsView,summaryListView,updateLineEntries. - Files:
MirroringWorkoutsSample/MirroringWorkoutsSampleApp.swift,Shared/WorkoutManager.swift,MirroringWorkoutsSample Watch App/PagingView.swift,MirroringWorkoutsSample Watch App/SummaryView.swift,MirroringWorkoutsSample/ChartView.swift,MirroringWorkoutsSample/SummaryView.swift.
Architecture takeaways
MirroringWorkoutsSampleAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, HealthKit, Charts, HealthKitUI 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 |
|---|---|
MirroringWorkoutsSample Watch App/MirroringWorkoutsSampleApp.swift |
Cited implementation, MirroringWorkoutsSampleWatchApp |
Shared/WorkoutManager.swift |
Cited implementation, ObservableObject, @Published, WorkoutManager, SessionSateChange, WorkoutElapsedTime |
MirroringWorkoutsSample Watch App/ActivityRingsView.swift |
Cited implementation, DispatchQueue.main.async, SwiftUI state property wrapper, SwiftUI, HealthKit, Foundation, ActivityRingsView |
MirroringWorkoutsSample Watch App/ControlsView.swift |
Cited implementation, ControlsView |
MirroringWorkoutsSample Watch App/AppDelegate.swift |
Cited implementation, Task, await suspension point, os, AppDelegate |
MirroringWorkoutsSample/ChartView.swift |
Cited implementation, Charts, LineChartEntry, ChartView |
Shared/WorkoutManager+iOS.swift |
@MainActor, Task closure isolated to MainActor, Feature implementation |
MirroringWorkoutsSample/MirroringWorkoutsSampleApp.swift |
MirroringWorkoutsSampleApp |
MirroringWorkoutsSample Watch App/PagingView.swift |
PagingView, Tab |
MirroringWorkoutsSample Watch App/SummaryView.swift |
SummaryView, SummaryMetricView |
MirroringWorkoutsSample/SummaryView.swift |
SummaryView, GridItemView |
Shared/ElapsedTimeView.swift |
ElapsedTimeView, ElapsedTimeFormatter |
MirroringWorkoutsSample Watch App/MetricsView.swift |
MetricsView |
MirroringWorkoutsSample/MirroringWorkoutView.swift |
MirroringWorkoutView |
MirroringWorkoutsSample/StartView.swift |
StartView |
MirroringWorkoutsSample/WorkoutListView.swift |
WorkoutListView |