Code-along: Elevating an app with Swift concurrency
At a glance
| Item | Summary |
|---|---|
| Purpose | Code along with the WWDC presenter to elevate a SwiftUI app with Swift concurrency. |
| App architecture | A Swift sample bundle with entry-bearing project variants End, Start, each leading to SwiftUI APIs. |
| Main patterns | Model-View-ViewModel |
| Project style | 36 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: async declaration or closure, Sendable or @Sendable, @MainActor, Task closure isolated to MainActor, Task; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: @Observable, SwiftUI state property wrapper. |
| Key frameworks/packages | SwiftUI, PhotosUI, Accelerate, CoreImage, Foundation; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── End/
│ └── SwiftSticker/
│ ├── SwiftStickerApp.swift
│ ├── ViewModel/
│ │ └── StickerViewModel.swift
│ ├── Model/
│ │ ├── PhotoProcessor.swift
│ │ ├── ProcessedPhoto.swift
│ │ └── ColorExtractor.swift
│ └── Views/
│ └── PhotoPickerView.swift
└── Start/
└── SwiftSticker/
├── SwiftStickerApp.swift
├── ViewModel/
│ └── StickerViewModel.swift
├── Model/
│ ├── PhotoProcessor.swift
│ ├── ProcessedPhoto.swift
│ └── ColorExtractor.swift
└── Views/
└── PhotoPickerView.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 6 project/configuration file(s) and 36 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["End"]
V2["Start"]
Boundary["SwiftUI APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
End/SwiftSticker/SwiftStickerApp.swift:12 — architecture anchor
@main
struct SwiftStickerApp: App {
// ...
PhotoPickerView(
viewModel: StickerViewModel()
)
// ...
}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
StickerViewModel *-- Array : invalidPhotos
StickerViewModel o-- Selection : selection
PhotoPickerView *-- StickerViewModel : viewModel
PhotoProcessor *-- ColorExtractor : colorExtractor
Ownership evidence
End/SwiftSticker/ViewModel/StickerViewModel.swift:21 — stored dependency or nearest verified ownership anchor
@Observable
class StickerViewModel {
// ...
var invalidPhotos: [SelectedPhoto.ID] = []
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
StickerViewModel |
Array (invalidPhotos) |
owns value state | App/module collaborators |
StickerViewModel |
Selection (selection) |
stores or receives | App/module collaborators |
PhotoPickerView |
StickerViewModel (viewModel) |
owns wrapper-managed state | App/module collaborators |
PhotoProcessor |
ColorExtractor (colorExtractor) |
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 |
|---|---|---|---|
| Suspension boundary | async declaration or closure |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | End/SwiftSticker/Model/PhotoProcessor.swift:15 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | End/SwiftSticker/Model/SelectedPhoto.swift:38 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | End/SwiftSticker/ViewModel/StickerViewModel+Utilities.swift:17 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | End/SwiftSticker/ViewModel/StickerViewModel+Utilities.swift:17 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | End/SwiftSticker/ViewModel/StickerViewModel+Utilities.swift:17 |
@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
End/SwiftSticker/Model/PhotoProcessor.swift:15 — representative execution boundary
@concurrent
func process(data: Data) async -> ProcessedPhoto? {
async let sticker = extractSticker(from: data)
// ...
}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. | End/SwiftSticker/ViewModel/StickerViewModel.swift:12 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | End/SwiftSticker/ViewModifier/ConfigureGrid.swift:18 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | End/SwiftSticker/Extensions/Image+Data.swift:8 |
| Source import | PhotosUI |
The cited file imports this module; runtime use and architectural role are not inferred. | End/SwiftSticker/Model/PhotoProcessor.swift:9 |
| Source import | Accelerate |
The cited file imports this module; runtime use and architectural role are not inferred. | End/SwiftSticker/Model/ColorExtractor.swift:10 |
| Source import | CoreImage |
The cited file imports this module; runtime use and architectural role are not inferred. | End/SwiftSticker/Model/PhotoProcessor.swift:11 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | End/SwiftSticker/Model/ColorExtractor.swift:9 |
| Source import | simd |
The cited file imports this module; runtime use and architectural role are not inferred. | End/SwiftSticker/Model/ColorExtractor.swift:11 |
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
End/SwiftSticker/SwiftStickerApp.swift:13 — representative type boundary
@main
struct SwiftStickerApp: App {
// ...
viewModel: StickerViewModel()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
SwiftStickerApp |
Application entry and top-level composition | App |
SwiftStickerApp |
Application entry and top-level composition | App |
StickerViewModel |
UI-facing state and feature coordination | Concrete collaborators/imported frameworks |
StickerViewModel |
UI-facing state and feature coordination | Concrete collaborators/imported frameworks |
PhotoProcessor |
Owns a feature processing stage | Concrete collaborators/imported frameworks |
PhotoPickerView |
User-interface presentation and input forwarding | View |
PhotoProcessor |
Owns a feature processing stage | Concrete collaborators/imported frameworks |
PhotoPickerView |
User-interface presentation and input forwarding | View |
ProcessedPhoto |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
PhotoColorScheme |
Represents a feature value or composable behavior | 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 |
|---|---|---|---|
k (End/SwiftSticker/Model/ColorExtractor.swift:15) |
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. |
sourceImage (End/SwiftSticker/Model/ColorExtractor.swift:22) |
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. |
rgbImageFormat (End/SwiftSticker/Model/ColorExtractor.swift:39) |
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. |
distances (End/SwiftSticker/Model/ColorExtractor.swift:50) |
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
End/SwiftSticker/Model/ColorExtractor.swift:15 — representative boundary
nonisolated final class ColorExtractor {
private let k = 4
// ...
}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 | SwiftStickerApp |
The source’s App suffix makes this role explicit. |
| Owns a feature processing stage | PhotoProcessor |
The source’s Processor suffix makes this role explicit. |
| User-interface presentation and input forwarding | PhotoPickerView |
The source’s View suffix makes this role explicit. |
| UI-facing state and feature coordination | StickerViewModel |
The source’s ViewModel suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Model-View-ViewModel | End/SwiftSticker/ViewModel/StickerViewModel.swift:13 |
Role-named view models keep UI-facing state or coordination outside view declarations. |
Naming conventions
- Types: App: SwiftStickerApp; Processor: PhotoProcessor; View: PhotoPickerView; ViewModel: StickerViewModel.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
loadPhoto,processAllPhotos,process,extractColors,extractSticker. - Files:
End/SwiftSticker/SwiftStickerApp.swift,Start/SwiftSticker/SwiftStickerApp.swift,End/SwiftSticker/ViewModel/StickerViewModel.swift,Start/SwiftSticker/ViewModel/StickerViewModel.swift,End/SwiftSticker/Model/PhotoProcessor.swift,End/SwiftSticker/Views/PhotoPickerView.swift.
Architecture takeaways
SwiftStickerAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, PhotosUI, Accelerate, CoreImage 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 |
|---|---|
End/SwiftSticker/SwiftStickerApp.swift |
Cited implementation, SwiftStickerApp |
End/SwiftSticker/ViewModel/StickerViewModel.swift |
Cited implementation, StickerViewModel, @Observable |
End/SwiftSticker/Model/ColorExtractor.swift |
Cited implementation, Accelerate, Foundation, simd, ColorExtractor, Centroid |
End/SwiftSticker/Model/PhotoProcessor.swift |
async declaration or closure, PhotosUI, CoreImage, PhotoProcessor |
End/SwiftSticker/Model/SelectedPhoto.swift |
Sendable or @Sendable, SelectedPhoto |
End/SwiftSticker/ViewModel/StickerViewModel+Utilities.swift |
@MainActor, Task closure isolated to MainActor, Task, Feature implementation |
End/SwiftSticker/ViewModifier/ConfigureGrid.swift |
SwiftUI state property wrapper, ConfigureStickerGrid |
End/SwiftSticker/Extensions/Image+Data.swift |
SwiftUI, Feature implementation |
Start/SwiftSticker/SwiftStickerApp.swift |
SwiftStickerApp |
Start/SwiftSticker/ViewModel/StickerViewModel.swift |
StickerViewModel |
End/SwiftSticker/Views/PhotoPickerView.swift |
PhotoPickerView |
Start/SwiftSticker/Model/PhotoProcessor.swift |
PhotoProcessor |
Start/SwiftSticker/Views/PhotoPickerView.swift |
PhotoPickerView |
End/SwiftSticker/Model/ProcessedPhoto.swift |
ProcessedPhoto, PhotoColorScheme, ProcessedPhotoResult |
Start/SwiftSticker/Model/ProcessedPhoto.swift |
ProcessedPhoto, PhotoColorScheme, ProcessedPhotoResult |
Start/SwiftSticker/Model/ColorExtractor.swift |
ColorExtractor, Centroid |