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

Creating and updating a complication’s timeline

At a glance

Item Summary
Purpose Create complications that batch-load a timeline of future entries and run periodic background sessions to update the timeline.
App architecture A Swift sample with the source-visible chain CoffeeTrackerApp → ContentView → CoffeeDataStore → ClockKit APIs.
Main patterns View-controller organization, Delegate or data-source callbacks, Central store, SwiftUI environment injection, Publisher-backed observable state, Actor isolation
Project style 10 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: actor, @MainActor, async declaration or closure, Task.detached, Task; none alone proves a background thread.
State/event model Source-visible mechanisms: ObservableObject, @Published, NotificationCenter.
Key frameworks/packages SwiftUI, os, Foundation, ClockKit, HealthKit; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── Coffee Tracker WatchKit Extension/
│   ├── CoffeeTrackerApp.swift
│   ├── CoffeeTrackerView.swift
│   ├── ContentView.swift
│   ├── DrinkListView.swift
│   ├── ComplicationController.swift
│   ├── HealthKitController.swift
│   ├── HostingController.swift
│   ├── CoffeeData.swift
│   ├── Drink.swift
│   ├── DrinkType.swift
│   └── Coffee Tracker WatchKit Extension.entitlements
└── Coffee Tracker WatchKit App/
    └── Info.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 6 project/configuration file(s) and 11 source declaration(s).

Overall architecture

Reference code

Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift:10 — architecture anchor

@main
struct CoffeeTrackerApp: App {
    // ...
            NavigationView {
                ContentView()
            }
    // ...
}

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

Ownership and state

Ownership evidence

Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift:13 — stored dependency or nearest verified ownership anchor

struct CoffeeTrackerView: View {
    // ...
    @EnvironmentObject var coffeeData: CoffeeData
    // ...
}
Owner Object or state Relationship Mutation authority
CoffeeTrackerView CoffeeData (coffeeData) receives environment-provided state The environment provider is authoritative
ContentView Logger (logger) creates and retains Initialized by the owner; the binding is immutable
DrinkListView CoffeeData (coffeeData) receives environment-provided state The environment provider is authoritative
ComplicationController Logger (logger) creates and retains 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
Actor isolation actor The cited type is actor-isolated; this does not select a background thread. Coffee Tracker WatchKit Extension/CoffeeData.swift:14
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. Coffee Tracker WatchKit Extension/CoffeeData.swift:104
Suspension boundary async declaration or closure The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. Coffee Tracker WatchKit Extension/CoffeeData.swift:124
Detached task Task.detached The source creates a detached task; no specific operating-system thread is established. Coffee Tracker WatchKit Extension/CoffeeData.swift:215
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. Coffee Tracker WatchKit Extension/HealthKitController.swift:276

@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

Coffee Tracker WatchKit Extension/CoffeeData.swift:14 — representative execution boundary

private actor CoffeeDataStore {
    // ...
    let logger = Logger(subsystem: "com.example.apple-samplecode.Coffee-Tracker.watchkitapp.watchkitextension.CoffeeDataStore", category: "ModelIO")
    // ...
}

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 ObservableObject ObservableObject supplies an observation contract. Coffee Tracker WatchKit Extension/CoffeeData.swift:105
State propagation @Published A published property can emit owner-controlled changes. Coffee Tracker WatchKit Extension/CoffeeData.swift:120
State propagation NotificationCenter NotificationCenter distributes named process-local events. Coffee Tracker WatchKit Extension/CoffeeData.swift:297
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. Coffee Tracker WatchKit Extension/CoffeeData.swift:8
Source import os The cited file imports this module; runtime use and architectural role are not inferred. Coffee Tracker WatchKit Extension/CoffeeData.swift:10
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. Coffee Tracker WatchKit Extension/Drink.swift:8
Source import ClockKit The cited file imports this module; runtime use and architectural role are not inferred. Coffee Tracker WatchKit Extension/CoffeeData.swift:9
Source import HealthKit The cited file imports this module; runtime use and architectural role are not inferred. Coffee Tracker WatchKit Extension/HealthKitController.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

Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift:11 — representative type boundary

@main
struct CoffeeTrackerApp: App {
    // ...
                ContentView()
    // ...
}
Type Responsibility Depends on or conforms to
CoffeeTrackerApp Application entry and top-level composition App
CoffeeTrackerView User-interface presentation and input forwarding View
ContentView User-interface presentation and input forwarding View
DrinkListView User-interface presentation and input forwarding View
ComplicationController View lifecycle, callbacks, and feature coordination NSObject, CLKComplicationDataSource
HealthKitController View lifecycle, callbacks, and feature coordination Concrete collaborators/imported frameworks
HostingController View lifecycle, callbacks, and feature coordination WKHostingController
CoffeeDataStore Centralized state or persistence access Concrete collaborators/imported frameworks
CoffeeData Represents feature data ObservableObject
Drink Represents a feature value or composable behavior Hashable, Codable

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
floatFormatter (Coffee Tracker WatchKit Extension/CoffeeData.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.
savedValue (Coffee Tracker WatchKit Extension/CoffeeData.swift:19) 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.
dataURL (Coffee Tracker WatchKit Extension/CoffeeData.swift:88) 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.
store (Coffee Tracker WatchKit Extension/CoffeeData.swift:115) 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

Coffee Tracker WatchKit Extension/CoffeeData.swift:12 — representative boundary

private let floatFormatter = FloatingPointFormatStyle<Double>().precision(.significantDigits(1...3))

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 CoffeeTrackerApp The source’s App suffix makes this role explicit.
View lifecycle, callbacks, and feature coordination ComplicationController, HealthKitController, HostingController The source’s Controller suffix makes this role explicit.
Centralized state or persistence access CoffeeDataStore The source’s Store suffix makes this role explicit.
User-interface presentation and input forwarding CoffeeTrackerView, ContentView, DrinkListView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
View-controller organization Coffee Tracker WatchKit Extension/ComplicationController.swift:11 A controller is the verified coordination boundary; this is MVC-style only where a separate model is present.
Delegate or data-source callbacks Coffee Tracker WatchKit Extension/ComplicationController.swift:11 Callback protocols invert event delivery back into the sample’s owner.
Central store Coffee Tracker WatchKit Extension/CoffeeData.swift:14 A store-named type centralizes feature state or persistence.
SwiftUI environment injection Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift:13 The environment supplies state or a capability without threading it through every initializer.
Publisher-backed observable state Coffee Tracker WatchKit Extension/CoffeeData.swift:120 Published properties notify observers while mutation remains with the state object.
Actor isolation Coffee Tracker WatchKit Extension/CoffeeData.swift:14 A declared actor creates an explicit isolation boundary; its executor is not described as a background thread.

Naming conventions

  • Types: App: CoffeeTrackerApp; Controller: ComplicationController, HealthKitController, HostingController; Store: CoffeeDataStore; View: CoffeeTrackerView, ContentView, DrinkListView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: colorForCaffeineDose, colorForDailyDrinkCount, addDrink, timelineEndDate, privacyBehavior, complicationDescriptors, currentTimelineEntry, timelineEntries.
  • Files: Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift, Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift, Coffee Tracker WatchKit Extension/ContentView.swift, Coffee Tracker WatchKit Extension/DrinkListView.swift, Coffee Tracker WatchKit Extension/ComplicationController.swift, Coffee Tracker WatchKit Extension/HealthKitController.swift.

Architecture takeaways

  • CoffeeTrackerApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, ClockKit, 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
Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift Cited implementation, CoffeeTrackerApp
Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift Cited implementation, CoffeeTrackerView, CoffeeTrackerView_Previews
Coffee Tracker WatchKit Extension/CoffeeData.swift Cited implementation, CoffeeDataStore, actor, @MainActor, async declaration or closure, Task.detached, ObservableObject, @Published, NotificationCenter, SwiftUI, os, ClockKit, CoffeeData
Coffee Tracker WatchKit Extension/ComplicationController.swift ComplicationController, Cited implementation
Coffee Tracker WatchKit Extension/HealthKitController.swift Task, HealthKit, HealthKitController
Coffee Tracker WatchKit Extension/Drink.swift Foundation, Drink
Coffee Tracker WatchKit Extension/ContentView.swift ContentView, ContentView_Previews
Coffee Tracker WatchKit Extension/DrinkListView.swift DrinkListView, DrinkListView_Previews
Coffee Tracker WatchKit Extension/HostingController.swift HostingController
Coffee Tracker WatchKit Extension/DrinkType.swift DrinkType