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

Updating an app to use strict concurrency

At a glance

Item Summary
Purpose Use this code to follow along with a guide to migrating your code to take advantage of the full concurrency protection that the Swift 6 language mode provides.
App architecture A Swift sample bundle with entry-bearing project variants Original, Updated, each leading to SwiftUI APIs.
Main patterns View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks, Central store, SwiftUI environment injection, Publisher-backed observable state
Project style 22 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: async declaration or closure, Task, actor, @MainActor, Sendable or @Sendable; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, ObservableObject, @Published.
Key frameworks/packages SwiftUI, CoffeeKit, os, Foundation, ClockKit; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── Original/
│   └── Coffee Tracker WatchKit Extension/
│       ├── CoffeeTrackerApp.swift
│       ├── ExtensionDelegate.swift
│       ├── ContentView.swift
│       ├── CoffeeTrackerView.swift
│       ├── DrinkListView.swift
│       ├── ComplicationController.swift
│       └── HostingController.swift
└── Updated/
    └── Coffee Tracker WatchKit Extension/
        ├── CoffeeTrackerApp.swift
        ├── ExtensionDelegate.swift
        ├── ContentView.swift
        ├── CoffeeTrackerView.swift
        └── DrinkListView.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 11 project/configuration file(s) and 29 source declaration(s).

Overall architecture

Reference code

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

@main
struct CoffeeTrackerApp: App {

    @WKApplicationDelegateAdaptor private var appDelegate: ExtensionDelegate

    @SceneBuilder var body: some Scene {
        WindowGroup {
            NavigationView {
                ContentView()
            }
        }
    }
}

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

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

@main
struct CoffeeTrackerApp: App {
    // ...
    @WKApplicationDelegateAdaptor private var appDelegate: ExtensionDelegate
    // ...
}
Owner Object or state Relationship Mutation authority
CoffeeTrackerApp ExtensionDelegate (appDelegate) stores or receives Owning lexical scope
ExtensionDelegate Logger (logger) creates and retains Initialized by the owner; the binding is immutable
ExtensionDelegate Logger (scheduleLogger) creates and retains Initialized by the owner; the binding is immutable
ContentView Logger (logger) creates and retains 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
Suspension boundary async declaration or closure The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. Original/Coffee Tracker WatchKit Extension/ComplicationController.swift:19
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. Original/Coffee Tracker WatchKit Extension/ContentView.swift:41
Actor isolation actor The cited type is actor-isolated; this does not select a background thread. Original/CoffeeKit/CoffeeData.swift:15
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. Original/CoffeeKit/CoffeeData.swift:107
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. Updated/CoffeeKit/Drink.swift:12

@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

Original/Coffee Tracker WatchKit Extension/ComplicationController.swift:19 — representative execution boundary

    func timelineEndDate(for complication: CLKComplication) async -> Date? {
        // Indicate that the app can provide timeline entries for the next 24 hours.
        Date().addingTimeInterval(24.0 * 60.0 * 60.0)
    }

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. Original/Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift:14
State propagation ObservableObject ObservableObject supplies an observation contract. Original/Coffee Tracker WatchKit Extension/ContentView.swift:69
State propagation @Published A published property can emit owner-controlled changes. Original/Coffee Tracker WatchKit Extension/ContentView.swift:70
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. Original/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift:8
Source import CoffeeKit The cited file imports this module; runtime use and architectural role are not inferred. Original/Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift:8
Source import os The cited file imports this module; runtime use and architectural role are not inferred. Original/Coffee Tracker WatchKit Extension/ContentView.swift:10
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. Original/CoffeeKit/Drink.swift:8
Source import ClockKit The cited file imports this module; runtime use and architectural role are not inferred. Original/Coffee Tracker WatchKit Extension/ComplicationController.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

Updated/CoffeeKit/CoffeeData.swift:310 — representative type boundary

@MainActor
public protocol CaffeineThresholdDelegate: AnyObject {
    func caffeineLevel(at level: Double)
}
Type Responsibility Depends on or conforms to
CoffeeTrackerApp Application entry and top-level composition App
CoffeeTrackerApp Application entry and top-level composition App
CaffeineThresholdDelegate Defines a capability or collaboration contract AnyObject
CaffeineThresholdDelegate Defines a capability or collaboration contract AnyObject
ExtensionDelegate Receives callback-driven events NSObject, WKApplicationDelegate
ExtensionDelegate Receives callback-driven events NSObject, WKApplicationDelegate
ContentView User-interface presentation and input forwarding View
ContentView User-interface presentation and input forwarding View
CoffeeTrackerView User-interface presentation and input forwarding View
DrinkListView User-interface presentation and input forwarding View

The source explicitly defines local protocol relationships: RecaffeinaterCaffeineThresholdDelegate, RecaffeinaterCaffeineThresholdDelegate.

Access control

Symbol Access Verified effect Likely rationale
appDelegate (Original/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.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.
colorForCaffeineDose (Original/Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift:53) 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.
colorForDailyDrinkCount (Original/Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift:60) 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.
createTimelineEntry (Original/Coffee Tracker WatchKit Extension/ComplicationController.swift:98) 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

Original/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift:13 — representative boundary

@main
struct CoffeeTrackerApp: App {
    // ...
    @WKApplicationDelegateAdaptor private var appDelegate: ExtensionDelegate
    // ...
}

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.
Receives callback-driven events CaffeineThresholdDelegate, CoffeeLocationDelegate, ExtensionDelegate The source’s Delegate 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 Original/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.
Protocol-oriented abstraction Original/Coffee Tracker WatchKit Extension/ContentView.swift:74 A local protocol and concrete conformance create an explicit capability boundary.
Delegate or data-source callbacks Original/Coffee Tracker WatchKit Extension/ComplicationController.swift:11 Callback protocols invert event delivery back into the sample’s owner.
Central store Original/CoffeeKit/CoffeeData.swift:15 A store-named type centralizes feature state or persistence.
SwiftUI environment injection Original/Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift:14 The environment supplies state or a capability without threading it through every initializer.
Publisher-backed observable state Original/Coffee Tracker WatchKit Extension/ContentView.swift:70 Published properties notify observers while mutation remains with the state object.

Main application flow

Reference code

Updated/CoffeeKit/CoffeeData.swift:281load()

    func load() async {
        var drinks = await store.load()

        // Remove old drinks.
        drinks.removeOutdatedDrinks()

        // Assign loaded drinks to the model.
        currentDrinks = drinks
        await drinksUpdated()

        // Load new data from HealthKit.
        guard await healthKitController.requestAuthorization() else {
            logger.debug("Unable to authorize HealthKit.")
            return
        }

        await self.healthKitController.loadNewDataFromHealthKit()
    }

Naming conventions

  • Types: App: CoffeeTrackerApp; Controller: ComplicationController, HealthKitController, HostingController; Delegate: CaffeineThresholdDelegate, CoffeeLocationDelegate, ExtensionDelegate; Store: CoffeeDataStore; View: CoffeeTrackerView, ContentView, DrinkListView.
  • Protocols: CaffeineThresholdDelegate, CaffeineThresholdDelegate.
  • Methods: handle, scheduleBackgroundRefreshTasks, caffeineLevel, colorForCaffeineDose, colorForDailyDrinkCount, addDrink.
  • Files: Original/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift, Updated/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift, Original/Coffee Tracker WatchKit Extension/ExtensionDelegate.swift, Updated/Coffee Tracker WatchKit Extension/ExtensionDelegate.swift, Original/Coffee Tracker WatchKit Extension/ContentView.swift, Updated/Coffee Tracker WatchKit Extension/ContentView.swift.

Architecture takeaways

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

Source map

Source file Relevant symbols
Original/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift Cited implementation, SwiftUI, CoffeeTrackerApp
Updated/CoffeeKit/CoffeeData.swift CaffeineThresholdDelegate, CoffeeDataStore, CoffeeData, CoffeeLocationDelegate
Original/Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift Cited implementation, SwiftUI state property wrapper, CoffeeKit, CoffeeTrackerView, CoffeeTrackerView_Previews
Original/Coffee Tracker WatchKit Extension/ComplicationController.swift Cited implementation, ComplicationController, async declaration or closure, ClockKit
Original/Coffee Tracker WatchKit Extension/ContentView.swift Cited implementation, Task, ObservableObject, @Published, os, ContentView, Recaffeinater, ContentView_Previews
Original/CoffeeKit/CoffeeData.swift CoffeeDataStore, actor, @MainActor, CoffeeData, CaffeineThresholdDelegate
Updated/CoffeeKit/Drink.swift Sendable or @Sendable, Drink
Original/CoffeeKit/Drink.swift Foundation, Drink
Updated/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift CoffeeTrackerApp
Original/Coffee Tracker WatchKit Extension/ExtensionDelegate.swift ExtensionDelegate
Updated/Coffee Tracker WatchKit Extension/ExtensionDelegate.swift ExtensionDelegate
Updated/Coffee Tracker WatchKit Extension/ContentView.swift ContentView, Recaffeinater, ContentView_Previews
Original/Coffee Tracker WatchKit Extension/DrinkListView.swift DrinkListView, DrinkListView_Previews
Updated/Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift CoffeeTrackerView, CoffeeTrackerView_Previews
Updated/Coffee Tracker WatchKit Extension/DrinkListView.swift DrinkListView, DrinkListView_Previews
Original/Coffee Tracker WatchKit Extension/HostingController.swift HostingController