Updating an App to Use Swift Concurrency
At a glance
| Item | Summary |
|---|---|
| Purpose | Improve your app’s performance by refactoring your code to take advantage of asynchronous functions in Swift. |
| App architecture | A Swift sample bundle with entry-bearing project variants Starting Point, Updated, each leading to SwiftUI APIs. |
| Main patterns | View-controller organization, Delegate or data-source callbacks, Central store, SwiftUI environment injection, Publisher-backed observable state, Actor isolation |
| Project style | 22 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: DispatchQueue(label:), DispatchQueue.main.async, actor, @MainActor, async declaration or closure; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: ObservableObject, @Published, SwiftUI state property wrapper. |
| Key frameworks/packages | SwiftUI, os, Foundation, ClockKit, HealthKit; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── Starting Point/
│ └── Coffee Tracker WatchKit Extension/
│ ├── CoffeeTrackerApp.swift
│ ├── ExtensionDelegate.swift
│ ├── CoffeeTrackerView.swift
│ ├── ContentView.swift
│ ├── DrinkListView.swift
│ ├── ComplicationController.swift
│ └── HealthKitController.swift
└── Updated/
└── Coffee Tracker WatchKit Extension/
├── CoffeeTrackerApp.swift
├── ExtensionDelegate.swift
├── CoffeeTrackerView.swift
├── ContentView.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 23 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["Starting Point"]
V2["Updated"]
Boundary["SwiftUI APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
Starting Point/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift:10 — architecture anchor
@main
struct CoffeeTrackerApp: App {
@WKExtensionDelegateAdaptor 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
classDiagram
CoffeeTrackerApp o-- ExtensionDelegate : appDelegate
ExtensionDelegate *-- Logger : logger
ExtensionDelegate *-- Logger : scheduleLogger
CoffeeTrackerView o-- CoffeeData : coffeeData
Ownership evidence
Starting Point/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift:13 — stored dependency or nearest verified ownership anchor
@main
struct CoffeeTrackerApp: App {
// ...
@WKExtensionDelegateAdaptor 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 |
CoffeeTrackerView |
CoffeeData (coffeeData) |
receives environment-provided state | The environment provider is authoritative |
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 |
|---|---|---|---|
| Queue scheduling | DispatchQueue(label:) |
The source constructs a dispatch queue; its label alone does not prove a thread. | Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift:25 |
| Queue scheduling | DispatchQueue.main.async |
The source addresses the main dispatch queue. | Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift:224 |
| Actor isolation | actor |
The cited type is actor-isolated; this does not select a background thread. | Updated/Coffee Tracker WatchKit Extension/CoffeeData.swift:14 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | Updated/Coffee Tracker WatchKit Extension/CoffeeData.swift:98 |
| Suspension boundary | async declaration or closure |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | Updated/Coffee Tracker WatchKit Extension/CoffeeData.swift:116 |
@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
Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift:25 — representative execution boundary
class CoffeeData: ObservableObject {
// ...
private var background = DispatchQueue(label: "Background Queue", qos: .userInitiated)
// ...
}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. | Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift:15 |
| State propagation | @Published |
A published property can emit owner-controlled changes. | Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift:30 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | Starting Point/Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift:13 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift:8 |
| Source import | os |
The cited file imports this module; runtime use and architectural role are not inferred. | Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift:10 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Starting Point/Coffee Tracker WatchKit Extension/Drink.swift:8 |
| Source import | ClockKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift:9 |
| Source import | HealthKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Starting Point/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
Starting Point/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift:11 — representative type boundary
@main
struct CoffeeTrackerApp: App {
// ...
@WKExtensionDelegateAdaptor private var appDelegate: ExtensionDelegate
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
CoffeeTrackerApp |
Application entry and top-level composition | App |
CoffeeTrackerApp |
Application entry and top-level composition | App |
ExtensionDelegate |
Receives callback-driven events | NSObject, WKExtensionDelegate |
ExtensionDelegate |
Receives callback-driven events | NSObject, WKExtensionDelegate |
CoffeeTrackerView |
User-interface presentation and input forwarding | View |
ContentView |
User-interface presentation and input forwarding | View |
DrinkListView |
User-interface presentation and input forwarding | View |
CoffeeTrackerView |
User-interface presentation and input forwarding | View |
ContentView |
User-interface presentation and input forwarding | View |
DrinkListView |
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 |
|---|---|---|---|
floatFormatter (Starting Point/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. |
background (Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift:25) |
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. |
currentDrinks (Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift:30) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
savedValue (Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift:46) |
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
Starting Point/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. |
| Receives callback-driven events | 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 | Starting Point/Coffee Tracker WatchKit Extension/ComplicationController.swift:10 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Delegate or data-source callbacks | Starting Point/Coffee Tracker WatchKit Extension/ComplicationController.swift:10 |
Callback protocols invert event delivery back into the sample’s owner. |
| Central store | Updated/Coffee Tracker WatchKit Extension/CoffeeData.swift:14 |
A store-named type centralizes feature state or persistence. |
| SwiftUI environment injection | Starting Point/Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift:13 |
The environment supplies state or a capability without threading it through every initializer. |
| Publisher-backed observable state | Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift:30 |
Published properties notify observers while mutation remains with the state object. |
| Actor isolation | Updated/Coffee Tracker WatchKit Extension/CoffeeData.swift:14 |
A declared actor creates an explicit isolation boundary; its executor is not described as a background thread. |
Main application flow
sequenceDiagram
participant CoffeeData
participant store
participant healthKitController
CoffeeData->>store: load()
CoffeeData->>CoffeeData: drinksUpdated()
CoffeeData->>healthKitController: requestAuthorization()
CoffeeData->>CoffeeData: loadNewDataFromHealthKit()
Reference code
Updated/Coffee Tracker WatchKit Extension/CoffeeData.swift:266 — load()
func load() async {
var drinks = await store.load()
// Drop old drinks
drinks.removeOutdatedDrinks()
// Assign loaded drinks to 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: ExtensionDelegate; Store: CoffeeDataStore; View: CoffeeTrackerView, ContentView, DrinkListView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
handle,scheduleBackgroundRefreshTasks,colorForCaffeineDose,colorForDailyDrinkCount,addDrink. - Files:
Starting Point/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift,Updated/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift,Starting Point/Coffee Tracker WatchKit Extension/ExtensionDelegate.swift,Updated/Coffee Tracker WatchKit Extension/ExtensionDelegate.swift,Starting Point/Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift,Starting Point/Coffee Tracker WatchKit Extension/ContentView.swift.
Architecture takeaways
CoffeeTrackerAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, ClockKit, HealthKit, WatchKit 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 |
|---|---|
Starting Point/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift |
Cited implementation, CoffeeTrackerApp |
Starting Point/Coffee Tracker WatchKit Extension/CoffeeData.swift |
Cited implementation, DispatchQueue(label:), DispatchQueue.main.async, ObservableObject, @Published, SwiftUI, os, ClockKit, CoffeeData |
Starting Point/Coffee Tracker WatchKit Extension/ComplicationController.swift |
ComplicationController, Cited implementation |
Updated/Coffee Tracker WatchKit Extension/CoffeeData.swift |
CoffeeDataStore, actor, @MainActor, async declaration or closure, CoffeeData |
Starting Point/Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift |
Cited implementation, SwiftUI state property wrapper, CoffeeTrackerView, CoffeeTrackerView_Previews |
Starting Point/Coffee Tracker WatchKit Extension/Drink.swift |
Foundation, Drink |
Starting Point/Coffee Tracker WatchKit Extension/HealthKitController.swift |
HealthKit, HealthKitController |
Updated/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift |
CoffeeTrackerApp |
Starting Point/Coffee Tracker WatchKit Extension/ExtensionDelegate.swift |
ExtensionDelegate |
Updated/Coffee Tracker WatchKit Extension/ExtensionDelegate.swift |
ExtensionDelegate |
Starting Point/Coffee Tracker WatchKit Extension/ContentView.swift |
ContentView, ContentView_Previews |
Starting Point/Coffee Tracker WatchKit Extension/DrinkListView.swift |
DrinkListView, DrinkListView_Previews |
Updated/Coffee Tracker WatchKit Extension/CoffeeTrackerView.swift |
CoffeeTrackerView, CoffeeTrackerView_Previews |
Updated/Coffee Tracker WatchKit Extension/ContentView.swift |
ContentView, ContentView_Previews |
Updated/Coffee Tracker WatchKit Extension/DrinkListView.swift |
DrinkListView, DrinkListView_Previews |
Starting Point/Coffee Tracker WatchKit Extension/HostingController.swift |
HostingController |