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

Filtering and sorting persistent data

At a glance

Item Summary
Purpose Manage data store presentation using predicates and dynamic queries.
App architecture A Swift sample with the source-visible chain DataCacheAppContentViewViewModelSwiftData APIs.
Main patterns Model-View-ViewModel, Binding-based state propagation, Actor-isolated state
Project style 17 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

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 {
    // ...
}

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

    @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.

Class and protocol design

DataCache/DataCacheApp.swift:13 — representative type boundary

struct DataCacheApp: App {
    // ...
}
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

    @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-isolated state DataCache/Preview Content/PreviewSampleData.swift:12 Actor annotations make the concurrency ownership boundary explicit.

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 DataCacheApp
DataCache/Model/ViewModel.swift ViewModel
DataCache/Model/GeoFeatureCollection.swift GeoFeatureCollection, Feature, Properties, Geometry, DownloadError
DataCache/Detail/MapView.swift MapView
DataCache/Navigation/ContentView.swift ContentView
DataCache/Detail/QuakeMarker.swift QuakeMarker, QuakeCircle
DataCache/Navigation/SortButton.swift SortButton, SortParameter
DataCache/Model/Location.swift Location
DataCache/Model/Quake.swift Quake
DataCache/Navigation/DeleteButton.swift DeleteButton