Adopting SwiftData for a Core Data app
At a glance
| Item | Summary |
|---|---|
| Purpose | Persist data in your app intuitively with the Swift native persistence framework. |
| App architecture | A Swift sample bundle with entry-bearing project variants Trips-Coexistence, Trips-CoreData, Trips-SwiftData, each leading to CoreData APIs. |
| Main patterns | View-controller organization, Delegate or data-source callbacks, Actor-isolated state |
| Project style | 59 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
Project structure
Source bundle/
├── Trips-Coexistence/
│ ├── Trips/
│ │ ├── TripsApp.swift
│ │ └── BucketListView.swift
│ └── TripsWidget/
│ └── TripsWidgetBundle.swift
├── Trips-CoreData/
│ └── Trips/
│ ├── TripsApp.swift
│ └── BucketListView.swift
└── Trips-SwiftData/
├── Trips/
│ ├── TripsApp.swift
│ ├── ContentView.swift
│ ├── LocationSearchView.swift
│ └── BucketListView.swift
├── TripsWidget/
│ └── TripsWidgetBundle.swift
└── Shared/
├── DataModel.swift
└── Trip.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 17 project/configuration file(s) and 77 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["Trips-Coexistence"]
V2["Trips-CoreData"]
V3["Trips-SwiftData"]
Boundary["CoreData APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Bundle --> V3
V3 --> Boundary
Reference code
Trips-Coexistence/Trips/TripsApp.swift:10 — architecture anchor
@main
struct TripsApp: 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
BucketListView o-- CDTrip : trip
BucketListView o-- FetchedResults : bucketList
BucketListView o-- Bucketlist : _bucketList
BucketListItemToggle o-- CDBucketListItem : item
Ownership evidence
Trips-Coexistence/Trips/BucketListView.swift:12 — stored dependency or nearest verified ownership anchor
var trip: CDTrip
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest private var bucketList: FetchedResults<CDBucketListItem>
@State private var showAddItem = false| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
BucketListView |
CDTrip (trip) |
stores or receives | App/module collaborators |
BucketListView |
FetchedResults (bucketList) |
stores or receives | Owning lexical scope |
BucketListView |
Bucketlist (_bucketList) |
stores or receives | App/module collaborators |
BucketListItemToggle |
CDBucketListItem (item) |
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.
Class and protocol design
Trips-Coexistence/Trips/TripsApp.swift:11 — representative type boundary
struct TripsApp: App {
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
TripsApp |
Application entry and top-level composition | App |
TripsApp |
Application entry and top-level composition | App |
TripsApp |
Application entry and top-level composition | App |
ContentView |
User-interface presentation and input forwarding | View |
LocationSearchView |
User-interface presentation and input forwarding | View |
BucketListView |
User-interface presentation and input forwarding | View |
BucketListView |
User-interface presentation and input forwarding | View |
DataModel |
Feature data or observable state | Concrete collaborators/imported frameworks |
BucketListView |
User-interface presentation and input forwarding | View |
TripListView |
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 |
|---|---|---|---|
dismiss (Trips-Coexistence/Trips/AddBucketListItemView.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. |
viewContext (Trips-Coexistence/Trips/AddBucketListItemView.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. |
title (Trips-Coexistence/Trips/AddBucketListItemView.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. |
details (Trips-Coexistence/Trips/AddBucketListItemView.swift:17) |
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
Trips-Coexistence/Trips/AddBucketListItemView.swift:13 — representative boundary
@Environment(\.dismiss) private var dismiss
@Environment(\.managedObjectContext) private var viewContext
@State private var title = ""
@State private var details = ""
@State private var hasReservations = falseSwift 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 | TripsApp |
The source’s App suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | MapCameraController, PersistenceController |
The source’s Controller suffix makes this role explicit. |
| Feature data or observable state | DataModel |
The source’s Model suffix makes this role explicit. |
| Supplies a capability or framework resource | Provider |
The source’s Provider suffix makes this role explicit. |
| User-interface presentation and input forwarding | AddBucketListItemView, AddTripView, BucketListItemView, BucketListView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | Trips-SwiftData/Trips/MapCameraController.swift:14 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Delegate or data-source callbacks | Trips-SwiftData/Trips/LocationSearchView.swift:12 |
Callback protocols invert event delivery back into the sample’s owner. |
| Actor-isolated state | Trips-Coexistence/TripsWidget/PreviewSampleData.swift:14 |
Actor annotations make the concurrency ownership boundary explicit. |
Naming conventions
- Types: App: TripsApp; Controller: MapCameraController, PersistenceController; Model: DataModel; Provider: Provider; View: AddBucketListItemView, AddTripView, BucketListItemView, BucketListView, ContentView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
completerDidUpdateResults,completer,selectCompletion,deleteItems,saveContext. - Files:
Trips-Coexistence/Trips/TripsApp.swift,Trips-CoreData/Trips/TripsApp.swift,Trips-SwiftData/Trips/TripsApp.swift,Trips-Coexistence/TripsWidget/TripsWidgetBundle.swift,Trips-SwiftData/TripsWidget/TripsWidgetBundle.swift,Trips-SwiftData/Trips/ContentView.swift.
Architecture takeaways
TripsAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, SwiftData, WidgetKit, CoreData 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 |
|---|---|
Trips-Coexistence/Trips/TripsApp.swift |
TripsApp |
Trips-CoreData/Trips/TripsApp.swift |
TripsApp |
Trips-SwiftData/Trips/TripsApp.swift |
TripsApp |
Trips-Coexistence/TripsWidget/TripsWidgetBundle.swift |
TripsWidgetBundle |
Trips-SwiftData/TripsWidget/TripsWidgetBundle.swift |
TripsWidgetBundle |
Trips-SwiftData/Trips/ContentView.swift |
ContentView, Segment, SortOption, GroupOption |
Trips-SwiftData/Trips/LocationSearchView.swift |
LocationSearchCompleter, LocationSearchView, LocationSearchSheet, CompletionLabel |
Trips-Coexistence/Trips/BucketListView.swift |
BucketListView, BucketListItemToggle |
Trips-CoreData/Trips/BucketListView.swift |
BucketListView, BucketListItemToggle |
Trips-SwiftData/Shared/DataModel.swift |
DataModel, TransactionAuthor |