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

Reading and Writing HealthKit Series Data

At a glance

Item Summary
Purpose Share and read heartbeat and quantity series data using series builders and queries.
App architecture A Swift sample with the source-visible chain AppDelegateDetailViewControllerGameControllerHeartSensorSessionHealthKit APIs.
Main patterns View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks
Project style 10 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── HeartDogs/
│   ├── AppDelegate.swift
│   ├── GameControllerHeartSensorSession.swift
│   ├── GameViewController.swift
│   ├── DetailViewController.swift
│   ├── HistoryViewController.swift
│   ├── Detailable.swift
│   ├── HealthStoreContainer.swift
│   ├── HKHeartbeatSeriesSample+Detailable.swift
│   ├── HKStatistics+Detailable.swift
│   └── UIViewController+Enumerate.swift
├── Configuration/
│   └── SampleCode.xcconfig
└── HeartDogs.xcodeproj/
    └── .xcodesamplecode.plist

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 7 project/configuration file(s) and 9 source declaration(s).

Overall architecture

Reference code

HeartDogs/AppDelegate.swift:11 — architecture anchor

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    // ...
}

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 HealthKit.

Ownership and state

Ownership evidence

HeartDogs/AppDelegate.swift:14 — stored dependency or nearest verified ownership anchor

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    // ...
    var window: UIWindow?
    // ...
}
Owner Object or state Relationship Mutation authority
AppDelegate UIWindow (window) stores or receives App/module collaborators
AppDelegate HKHealthStore (healthStore) stores or receives Initialized by the owner; the binding is immutable
GameControllerHeartSensorSession NSUUID (UUID) creates and retains Initialized by the owner; the binding is immutable
GameControllerHeartSensorSession Date (startDate) 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.

Class and protocol design

HeartDogs/GameControllerHeartSensorSession.swift:11 — representative type boundary

protocol GameControllerHeartSensorSessionDelegate: AnyObject {
    func sessionDidStart(_ session: GameControllerHeartSensorSession, startDate: Date)
    func sessionDidReceiveHeartRate(_ session: GameControllerHeartSensorSession, heartRate: HKQuantity, dateInterval: DateInterval)
    func sessionDidReceiveHeartBeat(_ session: GameControllerHeartSensorSession, timeIntervalSinceStart: TimeInterval, precededByGap: Bool)
    func sessionDidEnd(_ session: GameControllerHeartSensorSession, endDate: Date)
}
Type Responsibility Depends on or conforms to
AppDelegate Receives callback-driven events UIResponder, UIApplicationDelegate
GameControllerHeartSensorSessionDelegate Defines a capability or collaboration contract AnyObject
GameControllerHeartSensorSession Owns a session-scoped interaction Concrete collaborators/imported frameworks
GameViewController View lifecycle, callbacks, and feature coordination UIViewController, HealthStoreContainer, GameControllerHeartSensorSessionDelegate
DetailViewController View lifecycle, callbacks, and feature coordination UITableViewController, HealthStoreContainer
HistoryViewController View lifecycle, callbacks, and feature coordination UITableViewController, HealthStoreContainer
Detailable Defines a capability or collaboration contract Concrete collaborators/imported frameworks
HealthStoreContainer Defines a capability or collaboration contract Concrete collaborators/imported frameworks
GameState Represents mutable feature state Concrete collaborators/imported frameworks

The source explicitly defines local protocol relationships: DetailViewControllerHealthStoreContainer, GameViewControllerHealthStoreContainer, GameViewControllerGameControllerHeartSensorSessionDelegate, HistoryViewControllerHealthStoreContainer, HKHeartbeatSeriesSampleDetailable, HKStatisticsDetailable.

Access control

Symbol Access Verified effect Likely rationale
healthStore (HeartDogs/AppDelegate.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.
overviewStrings (HeartDogs/DetailViewController.swift:14) 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.
detailStrings (HeartDogs/DetailViewController.swift:15) 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.
configureView (HeartDogs/DetailViewController.swift:25) 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

HeartDogs/AppDelegate.swift:16 — representative boundary

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    // ...
    private let healthStore: HKHealthStore
    // ...
}

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
View lifecycle, callbacks, and feature coordination DetailViewController, GameViewController, HistoryViewController The source’s Controller suffix makes this role explicit.
Receives callback-driven events AppDelegate, GameControllerHeartSensorSessionDelegate The source’s Delegate suffix makes this role explicit.
Owns a session-scoped interaction GameControllerHeartSensorSession The source’s Session suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
View-controller organization HeartDogs/DetailViewController.swift:11 A controller is the verified coordination boundary; this is MVC-style only where a separate model is present.
Protocol-oriented abstraction HeartDogs/DetailViewController.swift:11 A local protocol and concrete conformance create an explicit capability boundary.
Delegate or data-source callbacks HeartDogs/AppDelegate.swift:12 Callback protocols invert event delivery back into the sample’s owner.

Naming conventions

  • Types: Controller: DetailViewController, GameViewController, HistoryViewController; Delegate: AppDelegate, GameControllerHeartSensorSessionDelegate; Session: GameControllerHeartSensorSession.
  • Protocols: GameControllerHeartSensorSessionDelegate, Detailable, HealthStoreContainer.
  • Methods: application, sessionDidStart, sessionDidReceiveHeartRate, sessionDidReceiveHeartBeat, sessionDidEnd, start, finish, generateSimulatedHeartData.
  • Files: HeartDogs/AppDelegate.swift, HeartDogs/GameControllerHeartSensorSession.swift, HeartDogs/GameViewController.swift, HeartDogs/DetailViewController.swift, HeartDogs/HistoryViewController.swift, HeartDogs/Detailable.swift.

Architecture takeaways

  • AppDelegate is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches HealthKit, UIKit 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.
  • Local protocol relationships provide an explicit substitution boundary.

Source map

Source file Relevant symbols
HeartDogs/AppDelegate.swift AppDelegate
HeartDogs/GameControllerHeartSensorSession.swift GameControllerHeartSensorSessionDelegate, GameControllerHeartSensorSession
HeartDogs/GameViewController.swift GameViewController, GameState
HeartDogs/DetailViewController.swift DetailViewController
HeartDogs/HistoryViewController.swift HistoryViewController
HeartDogs/Detailable.swift Detailable
HeartDogs/HealthStoreContainer.swift HealthStoreContainer
HeartDogs/HKHeartbeatSeriesSample+Detailable.swift Feature implementation
HeartDogs/HKStatistics+Detailable.swift Feature implementation
HeartDogs/UIViewController+Enumerate.swift Feature implementation