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

Tracking heart rate zones for workouts

At a glance

Item Summary
Purpose Start a workout on iOS or watchOS and track and display heart rate zones.
App architecture A Swift sample bundle with entry-bearing project variants HKWorkoutZoneSample Watch App, HKWorkoutZoneSampleApp, each leading to HealthKit APIs.
Main patterns Delegate or data-source callbacks
Project style 21 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: @MainActor, Task closure isolated to MainActor, Task, Sendable or @Sendable, await suspension point; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, @Observable.
Key frameworks/packages SwiftUI, HealthKit, Foundation, Combine; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── HKWorkoutZoneSample Watch App/
│   ├── HKWorkoutZoneSampleApp.swift
│   ├── MetricsView.swift
│   ├── ControlsView.swift
│   ├── SessionView.swift
│   └── StartView.swift
├── HKWorkoutZoneSampleApp/
│   ├── HKWorkoutZoneSampleApp.swift
│   └── MetricsView.swift
└── Shared/
    ├── Views/
    │   ├── SummaryView.swift
    │   ├── ElapsedTimeView.swift
    │   └── HeartRateZoneView.swift
    ├── WorkoutManager.swift
    └── NavigationModel.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 5 project/configuration file(s) and 30 source declaration(s).

Overall architecture

Reference code

HKWorkoutZoneSample Watch App/HKWorkoutZoneSampleApp.swift:10 — architecture anchor

@main
struct HKWorkoutZoneSampleWatchApp: App {
    @State private var workoutManager = WorkoutManager.shared

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(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

Ownership evidence

Shared/Views/SummaryView.swift:123 — stored dependency or nearest verified ownership anchor

private struct ZoneDetailView: View {
    let title: String
    // ...
}
Owner Object or state Relationship Mutation authority
ZoneDetailView String (title) owns value state Initialized by the owner; the binding is immutable
ZoneDetailView ZoneDisplayData (zones) stores or receives Initialized by the owner; the binding is immutable
SummaryMetricRow String (title) owns value state Initialized by the owner; the binding is immutable
SummaryMetricRow String (value) owns value state 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
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. Shared/CountDownManager.swift:11
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. Shared/NavigationModel.swift:39
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. Shared/NavigationModel.swift:39
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. Shared/WorkoutManager.swift:14
Suspension boundary await suspension point The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. Shared/WorkoutManager.swift:40

@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

Shared/CountDownManager.swift:11 — representative execution boundary

@MainActor
@Observable final class CountDownManager {
    var timeRemaining: TimeInterval = 3
    // ...
}

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. HKWorkoutZoneSample Watch App/ControlsView.swift:12
State propagation @Observable Observation macro publishes source-visible changes. Shared/CountDownManager.swift:12
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. HKWorkoutZoneSample Watch App/ControlsView.swift:8
Source import HealthKit The cited file imports this module; runtime use and architectural role are not inferred. HKWorkoutZoneSample Watch App/ControlsView.swift:9
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. Shared/CountDownManager.swift:9
Source import Combine The cited file imports this module; runtime use and architectural role are not inferred. Shared/CountDownManager.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

HKWorkoutZoneSample Watch App/HKWorkoutZoneSampleApp.swift:11 — representative type boundary

@main
struct HKWorkoutZoneSampleWatchApp: App {
    @State private var workoutManager = WorkoutManager.shared
    // ...
}
Type Responsibility Depends on or conforms to
HKWorkoutZoneSampleWatchApp Application entry and top-level composition App
HKWorkoutZoneSampleApp Application entry and top-level composition App
SummaryView User-interface presentation and input forwarding View
ZoneDetailView User-interface presentation and input forwarding View
WorkoutManager Long-lived feature or framework coordination NSObject
MetricsView User-interface presentation and input forwarding View
MetricsView User-interface presentation and input forwarding View
NavigationModel Feature data or observable state Concrete collaborators/imported frameworks
ElapsedTimeView User-interface presentation and input forwarding View
HeartRateZoneView 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
workoutManager (HKWorkoutZoneSample Watch App/HKWorkoutZoneSampleApp.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 (HKWorkoutZoneSampleApp/HKWorkoutZoneSampleApp.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.
timerFinishedSubject (Shared/CountDownManager.swift:16) 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.
countDownConnector (Shared/CountDownManager.swift:21) 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

HKWorkoutZoneSample Watch App/HKWorkoutZoneSampleApp.swift:12 — representative boundary

@main
struct HKWorkoutZoneSampleWatchApp: App {
    @State private var workoutManager = WorkoutManager.shared
    // ...
}

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 HKWorkoutZoneSampleApp, HKWorkoutZoneSampleWatchApp The source’s App suffix makes this role explicit.
Long-lived feature or framework coordination CountDownManager, WorkoutManager The source’s Manager suffix makes this role explicit.
Feature data or observable state MetricsModel, NavigationModel The source’s Model suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, ControlsView, CountDownView, ElapsedTimeView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Delegate or data-source callbacks Shared/WorkoutManager.swift:225 Callback protocols invert event delivery back into the sample’s owner.

Naming conventions

  • Types: App: HKWorkoutZoneSampleApp, HKWorkoutZoneSampleWatchApp; Manager: CountDownManager, WorkoutManager; Model: MetricsModel, NavigationModel; View: ContentView, ControlsView, CountDownView, ElapsedTimeView, HeartRateZoneView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: zoneColor, formatZoneDuration, prepareWorkout, requestAuthorization, startWorkout, pause, resume, togglePause.
  • Files: HKWorkoutZoneSampleApp/HKWorkoutZoneSampleApp.swift, Shared/Views/SummaryView.swift, Shared/WorkoutManager.swift, HKWorkoutZoneSample Watch App/MetricsView.swift, HKWorkoutZoneSampleApp/MetricsView.swift, Shared/NavigationModel.swift.

Architecture takeaways

  • HKWorkoutZoneSampleApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, 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
HKWorkoutZoneSample Watch App/HKWorkoutZoneSampleApp.swift Cited implementation, HKWorkoutZoneSampleWatchApp
Shared/Views/SummaryView.swift Cited implementation, SummaryView, ZoneDetailView, SummaryMetricRow
HKWorkoutZoneSampleApp/HKWorkoutZoneSampleApp.swift Cited implementation, HKWorkoutZoneSampleApp
Shared/CountDownManager.swift Cited implementation, @MainActor, @Observable, Foundation, Combine, CountDownManager
Shared/WorkoutManager.swift Cited implementation, Sendable or @Sendable, await suspension point, ZoneDisplayData, WorkoutManager, SessionStateChange
Shared/NavigationModel.swift Task closure isolated to MainActor, Task, NavigationModel, NavigationState
HKWorkoutZoneSample Watch App/ControlsView.swift SwiftUI state property wrapper, SwiftUI, HealthKit, ControlsView
HKWorkoutZoneSample Watch App/MetricsView.swift MetricsView, MetricRow
HKWorkoutZoneSampleApp/MetricsView.swift MetricsView, CustomAlignment
Shared/Views/ElapsedTimeView.swift ElapsedTimeView, ElapsedTimeFormatter
Shared/Views/HeartRateZoneView.swift HeartRateZoneView, ZoneBarChart
HKWorkoutZoneSample Watch App/SessionView.swift SessionView
HKWorkoutZoneSample Watch App/StartView.swift StartView
HKWorkoutZoneSampleApp/ControlsView.swift ControlsView
HKWorkoutZoneSampleApp/SessionView.swift SessionView
HKWorkoutZoneSampleApp/StartView.swift StartView