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

Logging symptoms associated with a medication

At a glance

Item Summary
Purpose Fetch medications and dose events from the HealthKit store, and create symptom samples to associate with them.
App architecture A Swift sample with the source-visible chain MedsSymptomsLoggingAppTabsViewHealthStoreDoseEventProviderHealthKit / HealthKitUI APIs.
Main patterns Central store, Binding-based state propagation
Project style 23 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: @MainActor, Sendable or @Sendable, Task, await suspension point, Task closure isolated to MainActor; none alone proves a background thread.
State/event model Source-visible mechanisms: @Observable, SwiftUI state property wrapper.
Key frameworks/packages SwiftUI, HealthKit, Foundation, Charts, HealthKitUI; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── MedsSymptomsLoggingApp/
    ├── MedsSymptomsLoggingApp.swift
    ├── Views/
    │   ├── Charts/
    │   │   ├── MedicationChartsView+ChartsView.swift
    │   │   ├── MedicationChartsView.swift
    │   │   └── MedicationChartsView+MedicationSelectorView.swift
    │   ├── TabsView.swift
    │   └── HealthKitAuthorizationGatedView.swift
    ├── Models/
    │   ├── AnnotatedMedicationConceptModel.swift
    │   ├── DoseEventModel.swift
    │   ├── Stores/
    │   │   └── HealthStore.swift
    │   └── SymptomModel.swift
    └── Data Sources/
        ├── DoseEventProvider.swift
        └── MedicationProvider.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 4 project/configuration file(s) and 27 source declaration(s).

Overall architecture

Reference code

MedsSymptomsLoggingApp/MedsSymptomsLoggingApp.swift:11 — architecture anchor

@main
struct MedsSymptomsLoggingApp: App {
    private let healthStore = HealthStore.shared.healthStore

    @State var triggerMedicationsAuthorization: Bool = false
    @State var healthDataAuthorized: Bool?

    var body: some Scene {
        WindowGroup {
            TabsView(toggleHealthDataAuthorization: $triggerMedicationsAuthorization,
                     healthDataAuthorized: $healthDataAuthorized)
            .onAppear {
                triggerMedicationsAuthorization.toggle()
            }
            .healthDataAccessRequest(store: healthStore,
                                     objectType: .userAnnotatedMedicationType(),
                                     trigger: triggerMedicationsAuthorization,
                                     completion: { @Sendable result in
                Task { @MainActor in
                    switch result {
                    case .success:
                        healthDataAuthorized = true
                    case .failure(let error):
                        print("Error when requesting HealthKit read authorizations: \(error)")
                    }
                }
            })
        }
    }
}

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

MedsSymptomsLoggingApp/MedsSymptomsLoggingApp.swift:15 — stored dependency or nearest verified ownership anchor

@main
struct MedsSymptomsLoggingApp: App {
    // ...
    @State var triggerMedicationsAuthorization: Bool = false
    // ...
}
Owner Object or state Relationship Mutation authority
MedsSymptomsLoggingApp Bool (triggerMedicationsAuthorization) owns wrapper-managed state App/module collaborators
MedsSymptomsLoggingApp Bool (healthDataAuthorized) owns wrapper-managed state App/module collaborators
ChartsView DateConfiguration (dateConfiguration) borrows mutable state The upstream binding owner is authoritative
ChartsView Array (chartSeries) 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. MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:11
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:11
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:29
Suspension boundary await suspension point The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:30
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. MedsSymptomsLoggingApp/MedsSymptomsLoggingApp.swift:29

@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

MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:11 — representative execution boundary

@Observable @MainActor class DoseEventProvider: Sendable {
    let healthStore: HKHealthStore
    // ...
}

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 @Observable Observation macro publishes source-visible changes. MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:11
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. MedsSymptomsLoggingApp/MedsSymptomsLoggingApp.swift:15
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. MedsSymptomsLoggingApp/Data Sources/MedicationProvider.swift:9
Source import HealthKit The cited file imports this module; runtime use and architectural role are not inferred. MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:9
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:8
Source import Charts The cited file imports this module; runtime use and architectural role are not inferred. MedsSymptomsLoggingApp/Views/Charts/MedicationChartsView+ChartsView.swift:9
Source import HealthKitUI The cited file imports this module; runtime use and architectural role are not inferred. MedsSymptomsLoggingApp/Views/HealthKitAuthorizationGatedView.swift:9
Source import UIKit The cited file imports this module; runtime use and architectural role are not inferred. MedsSymptomsLoggingApp/Views/Today/MedicationListView.swift:9

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

MedsSymptomsLoggingApp/MedsSymptomsLoggingApp.swift:12 — representative type boundary

@main
struct MedsSymptomsLoggingApp: App {
    private let healthStore = HealthStore.shared.healthStore
    // ...
}
Type Responsibility Depends on or conforms to
MedsSymptomsLoggingApp Application entry and top-level composition App
ChartsView User-interface presentation and input forwarding View
DateIntervalPaginationView User-interface presentation and input forwarding View
DoseEventModel Feature data or observable state Sendable, Identifiable, Equatable, Hashable
HealthStore Centralized state or persistence access Sendable
SymptomModel Feature data or observable state Sendable, Identifiable, Hashable
MedicationChartsView User-interface presentation and input forwarding View
TabsView User-interface presentation and input forwarding View
DoseEventProvider Supplies a capability or framework resource Sendable
MedicationProvider Supplies a capability or framework resource Concrete collaborators/imported frameworks

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
doseEvents (MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:19) private(set) Read access follows the declaration; writes remain in the private scope. Inference: allow observation while reserving invariant-changing writes for the owner.
updatedDoseSampleCollection (MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:21) private(set) Read access follows the declaration; writes remain in the private scope. Inference: allow observation while reserving invariant-changing writes for the owner.
handleResultForLastLoggedDose (MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:146) 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.
handleResult (MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:171) 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

MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift:19 — representative boundary

@Observable @MainActor class DoseEventProvider: Sendable {
    // ...
    private(set) var doseEvents: [DoseEventModel] = []
    // ...
}

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 MedsSymptomsLoggingApp The source’s App suffix makes this role explicit.
Feature data or observable state DoseEventModel, SymptomModel The source’s Model suffix makes this role explicit.
Supplies a capability or framework resource DoseEventProvider, MedicationProvider The source’s Provider suffix makes this role explicit.
Centralized state or persistence access HealthStore The source’s Store suffix makes this role explicit.
User-interface presentation and input forwarding ArchivedMedicationView, ChartsView, DateIntervalPaginationView, DoseEventView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Central store MedsSymptomsLoggingApp/Models/Stores/HealthStore.swift:11 A store-named type centralizes feature state or persistence.
Binding-based state propagation MedsSymptomsLoggingApp/Views/Charts/MedicationChartsView+ChartsView.swift:16 A binding exposes controlled read/write access while the upstream owner remains authoritative.

Naming conventions

  • Types: App: MedsSymptomsLoggingApp; Model: DoseEventModel, SymptomModel; Provider: DoseEventProvider, MedicationProvider; Store: HealthStore; View: ArchivedMedicationView, ChartsView, DateIntervalPaginationView, DoseEventView, HealthKitAuthorizationGatedView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: chartPoints, chartPoint, decrement, increment, todayView, calendarChartsView.
  • Files: MedsSymptomsLoggingApp/MedsSymptomsLoggingApp.swift, MedsSymptomsLoggingApp/Models/DoseEventModel.swift, MedsSymptomsLoggingApp/Models/Stores/HealthStore.swift, MedsSymptomsLoggingApp/Models/SymptomModel.swift, MedsSymptomsLoggingApp/Views/Charts/MedicationChartsView.swift, MedsSymptomsLoggingApp/Views/TabsView.swift.

Architecture takeaways

  • MedsSymptomsLoggingApp is 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
MedsSymptomsLoggingApp/MedsSymptomsLoggingApp.swift Cited implementation, MedsSymptomsLoggingApp, Task closure isolated to MainActor, SwiftUI state property wrapper
MedsSymptomsLoggingApp/Data Sources/DoseEventProvider.swift Cited implementation, @MainActor, Sendable or @Sendable, Task, await suspension point, @Observable, HealthKit, Foundation, DoseEventProvider
MedsSymptomsLoggingApp/Models/Stores/HealthStore.swift HealthStore
MedsSymptomsLoggingApp/Views/Charts/MedicationChartsView+ChartsView.swift Cited implementation, Charts, ChartsView, ChartSeries, ChartPoint, DateIntervalPaginationView
MedsSymptomsLoggingApp/Data Sources/MedicationProvider.swift SwiftUI, MedicationProvider
MedsSymptomsLoggingApp/Views/HealthKitAuthorizationGatedView.swift HealthKitUI, HealthKitAuthorizationGatedView
MedsSymptomsLoggingApp/Views/Today/MedicationListView.swift UIKit, MedicationListView
MedsSymptomsLoggingApp/Models/AnnotatedMedicationConceptModel.swift AnnotatedMedicationConcept
MedsSymptomsLoggingApp/Models/DoseEventModel.swift DoseEventModel
MedsSymptomsLoggingApp/Models/SymptomModel.swift SymptomModel
MedsSymptomsLoggingApp/Views/Charts/MedicationChartsView.swift MedicationChartsView, DateConfiguration
MedsSymptomsLoggingApp/Views/TabsView.swift TabsView, TabKind
MedsSymptomsLoggingApp/Views/Charts/MedicationChartsView+MedicationSelectorView.swift MedicationSelectorView
MedsSymptomsLoggingApp/Views/Today/ArchivedMedicationView.swift ArchivedMedicationView
MedsSymptomsLoggingApp/Views/Today/DoseEventView.swift DoseEventView
MedsSymptomsLoggingApp/Views/Today/MedicationView.swift MedicationView