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-isolated state |
| Project style | 22 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
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 {
// ...
}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.
Class and protocol design
Starting Point/Coffee Tracker WatchKit Extension/CoffeeTrackerApp.swift:11 — representative type boundary
struct CoffeeTrackerApp: App {
// ...
}| 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-isolated state | Updated/Coffee Tracker WatchKit Extension/CoffeeData.swift:14 |
Actor annotations make the concurrency ownership boundary explicit. |
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 |
CoffeeTrackerApp |
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/CoffeeTrackerView.swift |
CoffeeTrackerView, CoffeeTrackerView_Previews |
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 |