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

Backyard Birds: Building an app with SwiftData and widgets

At a glance

Item Summary
Purpose Create an app with persistent data, interactive widgets, and an all new in-app purchase experience.
App architecture A Swift sample bundle with entry-bearing project variants Multiplatform, Watch, Widgets, each leading to SwiftUI APIs.
Main patterns Actor isolation
Project style 114 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: Task, await suspension point, Sendable or @Sendable, Task.detached, DispatchQueue.main.asyncAfter; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper.
Key frameworks/packages SwiftUI, SwiftData, BackyardBirdsData, Foundation, BackyardBirdsUI, Local package ../BackyardBirdsData, Local package ../LayeredArtworkLibrary; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── Multiplatform/
│   ├── BackyardBirdsApp.swift
│   └── Backyards/
│       └── BackyardDetailView.swift
├── Watch/
│   └── BackyardBirdsWatchApp.swift
├── Widgets/
│   ├── WidgetsBundle.swift
│   └── Backyard/
│       ├── BackyardSnapshotTimelineProvider.swift
│       └── BackyardWidgetView.swift
├── BackyardBirdsData/
│   └── General/
│       └── SeededRandomGenerator.swift
└── BackyardBirdsUI/
    ├── Backyard/
    │   ├── BackyardSkyView.swift
    │   ├── BackyardViewport.swift
    │   └── BackyardViewportLayout.swift
    ├── PlantView.swift
    └── Visitors/
        └── RecentBackyardVisitorsView.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 8 project/configuration file(s) and 135 source declaration(s).

Overall architecture

Reference code

Multiplatform/BackyardBirdsApp.swift:12 — architecture anchor

@main
struct BackyardBirdsApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .backyardBirdsShop()
                .backyardBirdsDataContainer()
        }
    }
}

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

BackyardBirdsData/Backyards/BackyardSnapshot.swift:11 — stored dependency or nearest verified ownership anchor

public struct BackyardSnapshot {
    public var backyard: Backyard
    // ...
}
Owner Object or state Relationship Mutation authority
BackyardSnapshot Backyard (backyard) stores or receives App/module collaborators
BackyardSnapshot Bird (visitingBird) stores or receives App/module collaborators
BackyardSnapshot TimeInterval (visitingBirdDuration) stores or receives App/module collaborators
BackyardSnapshot TimeInterval (timeInterval) stores or receives 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
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. BackyardBirdsData/General/ModelPreview.swift:44
Suspension boundary await suspension point The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. BackyardBirdsData/General/ModelPreview.swift:45
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. BackyardBirdsData/Store/PassIdentifiers.swift:10
Detached task Task.detached The source creates a detached task; no specific operating-system thread is established. Multiplatform/Account/RestorePurchasesButton.swift:17
Queue scheduling DispatchQueue.main.asyncAfter The source addresses the main dispatch queue. Multiplatform/Backyards/BackyardDetailView.swift:50

@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

BackyardBirdsData/General/ModelPreview.swift:44 — representative execution boundary

                    Task {
                        try await Task.sleep(for: .seconds(1))
                        waitedToShowIssue = true
                    }

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. BackyardBirdsData/General/BackyardBirdsDataContainer.swift:26
Package manifest Local package ../BackyardBirdsData The manifest declares a local path dependency; direct target use and transitive position are not inferred. BackyardBirdsUI/Package.swift:29
Package manifest Local package ../LayeredArtworkLibrary The manifest declares a local path dependency; direct target use and transitive position are not inferred. BackyardBirdsUI/Package.swift:30
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. BackyardBirdsData/Backyards/BackyardTimeOfDay.swift:9
Source import SwiftData The cited file imports this module; runtime use and architectural role are not inferred. BackyardBirdsData/Account/Account+DataGeneration.swift:10
Source import BackyardBirdsData The cited file imports this module; runtime use and architectural role are not inferred. BackyardBirdsUI/Backyard/BackyardList.swift:11
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. BackyardBirdsData/Account/Account+DataGeneration.swift:8
Source import BackyardBirdsUI The cited file imports this module; runtime use and architectural role are not inferred. Multiplatform/Account/AccountNavigationStack.swift:10

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

Multiplatform/BackyardBirdsApp.swift:13 — representative type boundary

@main
struct BackyardBirdsApp: App {
    // ...
            ContentView()
    // ...
}
Type Responsibility Depends on or conforms to
BackyardBirdsApp Application entry and top-level composition App
BackyardBirdsWatchApp Application entry and top-level composition App
BackyardSnapshotTimelineProvider Supplies a capability or framework resource AppIntentTimelineProvider
BackyardWidgetView User-interface presentation and input forwarding View
BackyardSnapshotWidgetView User-interface presentation and input forwarding View
SeededRandomGenerator Generates feature data or resources RandomNumberGenerator
BackyardSkyView User-interface presentation and input forwarding View
PlantView User-interface presentation and input forwarding View
RecentBackyardVisitorsView User-interface presentation and input forwarding View
BackyardDetailView 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
logger (BackyardBirdsData/Account/Account+DataGeneration.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.
logger (BackyardBirdsData/Account/Account.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.
id (BackyardBirdsData/Account/Account.swift:17) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
bird (BackyardBirdsData/Account/Account.swift:18) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.

Reference code

BackyardBirdsData/Account/Account+DataGeneration.swift:12 — representative boundary

private let logger = Logger(subsystem: "BackyardBirdsData", category: "Account Generation")

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 BackyardBirdsApp, BackyardBirdsWatchApp The source’s App suffix makes this role explicit.
Generates feature data or resources SeededRandomGenerator The source’s Generator suffix makes this role explicit.
Supplies a capability or framework resource BackyardSnapshotTimelineProvider The source’s Provider suffix makes this role explicit.
User-interface presentation and input forwarding AppTabView, BackyardDetailView, BackyardSkyView, BackyardSnapshotWidgetView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Actor isolation Multiplatform/Shop/BirdBrain.swift:16 A declared actor creates an explicit isolation boundary; its executor is not described as a background thread.

Naming conventions

  • Types: App: BackyardBirdsApp, BackyardBirdsWatchApp; Generator: SeededRandomGenerator; Provider: BackyardSnapshotTimelineProvider; View: AppTabView, BackyardDetailView, BackyardSkyView, BackyardSnapshotWidgetView, BackyardTabView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: backyards, placeholder, snapshot, timeline, recommendations, next, body, backyardViewportContent.
  • Files: Multiplatform/BackyardBirdsApp.swift, Watch/BackyardBirdsWatchApp.swift, Widgets/WidgetsBundle.swift, Widgets/Backyard/BackyardSnapshotTimelineProvider.swift, Widgets/Backyard/BackyardWidgetView.swift, BackyardBirdsData/General/SeededRandomGenerator.swift.

Architecture takeaways

  • BackyardBirdsApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, SwiftData, BackyardBirdsData, BackyardBirdsUI 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
Multiplatform/BackyardBirdsApp.swift Cited implementation, BackyardBirdsApp
BackyardBirdsData/Backyards/BackyardSnapshot.swift Cited implementation, BackyardSnapshot, NotableEvent
BackyardBirdsData/Account/Account+DataGeneration.swift Cited implementation, SwiftData, Foundation
BackyardBirdsData/Account/Account.swift Cited implementation, Account
Multiplatform/Shop/BirdBrain.swift BirdBrain
BackyardBirdsData/General/ModelPreview.swift Task, await suspension point, ModelPreview, PreviewContentView
BackyardBirdsData/Store/PassIdentifiers.swift Sendable or @Sendable, PassIdentifiers, PassIDsKey
Multiplatform/Account/RestorePurchasesButton.swift Task.detached, RestorePurchasesButton
Multiplatform/Backyards/BackyardDetailView.swift DispatchQueue.main.asyncAfter, BackyardDetailView
BackyardBirdsData/General/BackyardBirdsDataContainer.swift SwiftUI state property wrapper, BackyardBirdsDataContainerViewModifier, GenerateDataViewModifier
BackyardBirdsUI/Package.swift Cited implementation
BackyardBirdsData/Backyards/BackyardTimeOfDay.swift SwiftUI, BackyardTimeOfDay
BackyardBirdsUI/Backyard/BackyardList.swift BackyardBirdsData, BackyardList
Multiplatform/Account/AccountNavigationStack.swift BackyardBirdsUI, AccountNavigationStack
Watch/BackyardBirdsWatchApp.swift BackyardBirdsWatchApp
Widgets/WidgetsBundle.swift WidgetsBundle