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

Maintaining a local copy of server data

At a glance

Item Summary
Purpose Create and update a persistent store to cache read-only network data.
App architecture A Swift sample with the source-visible chain DataCacheAppContentViewViewModelSwiftData APIs.
Main patterns Model-View-ViewModel, Binding-based state propagation, Actor isolation
Project style 17 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: async declaration or closure, @MainActor, Task, actor, Task closure isolated to MainActor; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, @Observable.
Key frameworks/packages SwiftUI, SwiftData, Foundation, MapKit, CoreLocation; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── DataCache/
    ├── DataCacheApp.swift
    ├── Model/
    │   ├── ViewModel.swift
    │   ├── GeoFeatureCollection.swift
    │   ├── Location.swift
    │   └── Quake.swift
    ├── Detail/
    │   ├── MapView.swift
    │   └── QuakeMarker.swift
    ├── Navigation/
    │   ├── ContentView.swift
    │   ├── SortButton.swift
    │   ├── DeleteButton.swift
    │   └── RefreshButton.swift
    └── Preview Content/
        └── ModelContainerPreviews.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 4 project/configuration file(s) and 22 source declaration(s).

Overall architecture

Reference code

DataCache/DataCacheApp.swift:12 — architecture anchor

@main
struct DataCacheApp: App {
    @State private var viewModel = ViewModel()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(viewModel)
        }
        .modelContainer(for: Quake.self)
    }
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into SwiftData.

Ownership and state

Ownership evidence

DataCache/DataCacheApp.swift:14 — stored dependency or nearest verified ownership anchor

@main
struct DataCacheApp: App {
    @State private var viewModel = ViewModel()
    // ...
}
Owner Object or state Relationship Mutation authority
DataCacheApp ViewModel (viewModel) owns wrapper-managed state Owning lexical scope
ViewModel SortParameter (sortParameter) stores or receives App/module collaborators
ViewModel SortOrder (sortOrder) stores or receives App/module collaborators
ViewModel String (searchText) owns value state 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. DataCache/Model/GeoFeatureCollection.swift:84
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. DataCache/Model/Quake+GeoFeatureCollection.swift:32
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. DataCache/Navigation/RefreshButton.swift:19
Actor isolation actor The cited type is actor-isolated; this does not select a background thread. DataCache/Preview Content/PreviewSampleData.swift:12
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. DataCache/Preview Content/PreviewSampleData.swift:32

@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

DataCache/Model/GeoFeatureCollection.swift:84 — representative execution boundary

    static func fetchFeatures() async throws -> GeoFeatureCollection {
        // ...
        let url = URL(string: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson")!
        // ...
    }

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. DataCache/DataCacheApp.swift:14
State propagation @Observable Observation macro publishes source-visible changes. DataCache/Model/ViewModel.swift:12
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. DataCache/DataCacheApp.swift:8
Source import SwiftData The cited file imports this module; runtime use and architectural role are not inferred. DataCache/DataCacheApp.swift:9
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. DataCache/Model/GeoFeatureCollection.swift:8
Source import MapKit The cited file imports this module; runtime use and architectural role are not inferred. DataCache/Detail/MapView.swift:10
Source import CoreLocation The cited file imports this module; runtime use and architectural role are not inferred. DataCache/Model/Location.swift:8
Source import OSLog The cited file imports this module; runtime use and architectural role are not inferred. DataCache/Model/Quake+GeoFeatureCollection.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

DataCache/DataCacheApp.swift:13 — representative type boundary

@main
struct DataCacheApp: App {
    @State private var viewModel = ViewModel()
    // ...
}
Type Responsibility Depends on or conforms to
DataCacheApp Application entry and top-level composition App
ViewModel UI-facing state and feature coordination Concrete collaborators/imported frameworks
MapView User-interface presentation and input forwarding View
ContentView User-interface presentation and input forwarding View
GeoFeatureCollection Represents a feature value or composable behavior Decodable
Feature Represents a feature value or composable behavior Decodable
Properties Represents a feature value or composable behavior Decodable
Geometry Represents a feature value or composable behavior Decodable
DownloadError Represents feature failure conditions Error
QuakeMarker Represents a feature value or composable behavior MapContent

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
viewModel (DataCache/DataCacheApp.swift:14) 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.
modelContext (DataCache/Detail/MapView.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.
quakes (DataCache/Detail/MapView.swift:16) 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.
distance (DataCache/Detail/MapView.swift:77) fileprivate Use is restricted to this source file. Inference: share with same-file helpers or extensions without exposing the symbol module-wide.

Reference code

DataCache/DataCacheApp.swift:14 — representative boundary

@main
struct DataCacheApp: App {
    @State private var viewModel = ViewModel()
    // ...
}

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 DataCacheApp The source’s App suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, MapView The source’s View suffix makes this role explicit.
UI-facing state and feature coordination ViewModel The source’s ViewModel suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Model-View-ViewModel DataCache/Model/ViewModel.swift:13 Role-named view models keep UI-facing state or coordination outside view declarations.
Binding-based state propagation DataCache/Detail/MapView.swift:18 A binding exposes controlled read/write access while the upstream owner remains authoritative.
Actor isolation DataCache/Preview Content/PreviewSampleData.swift:12 A declared actor creates an explicit isolation boundary; its executor is not described as a background thread.

Naming conventions

  • Types: App: DataCacheApp; View: ContentView, MapView; ViewModel: ViewModel.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: update, fetchFeatures, distance.
  • Files: DataCache/DataCacheApp.swift, DataCache/Model/ViewModel.swift, DataCache/Model/GeoFeatureCollection.swift, DataCache/Detail/MapView.swift, DataCache/Navigation/ContentView.swift, DataCache/Detail/QuakeMarker.swift.

Architecture takeaways

  • DataCacheApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, SwiftData, MapKit, CoreLocation 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
DataCache/DataCacheApp.swift Cited implementation, DataCacheApp, SwiftUI state property wrapper, SwiftUI, SwiftData
DataCache/Detail/MapView.swift Cited implementation, MapKit, MapView
DataCache/Model/ViewModel.swift ViewModel, @Observable
DataCache/Preview Content/PreviewSampleData.swift PreviewSampleData, actor, Task closure isolated to MainActor
DataCache/Model/GeoFeatureCollection.swift async declaration or closure, Foundation, GeoFeatureCollection, Feature, Properties, Geometry, DownloadError
DataCache/Model/Quake+GeoFeatureCollection.swift @MainActor, OSLog, Feature implementation
DataCache/Navigation/RefreshButton.swift Task, RefreshButton
DataCache/Model/Location.swift CoreLocation, Location
DataCache/Navigation/ContentView.swift ContentView
DataCache/Detail/QuakeMarker.swift QuakeMarker, QuakeCircle
DataCache/Navigation/SortButton.swift SortButton, SortParameter
DataCache/Model/Quake.swift Quake
DataCache/Navigation/DeleteButton.swift DeleteButton
DataCache/Preview Content/ModelContainerPreviews.swift ModelContainerPreview
DataCache/Sidebar/QuakeList.swift QuakeList
DataCache/Sidebar/QuakeListInfo.swift QuakeListInfo