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 isolation |
| Project style | 59 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: actor, @MainActor, Task closure isolated to MainActor, Task, await suspension point; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, NotificationCenter, @Observable. |
| Key frameworks/packages | SwiftUI, SwiftData, WidgetKit, CoreData, MapKit; these are source dependencies, not architecture labels. |
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 {
let persistenceController = PersistenceController.shared
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext,
persistenceController.container.viewContext)
}
}
}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
struct BucketListView: View {
var trip: CDTrip
// ...
}| 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.
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 |
|---|---|---|---|
| Actor isolation | actor |
The cited type is actor-isolated; this does not select a background thread. | Trips-Coexistence/TripsWidget/PreviewSampleData.swift:14 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | Trips-Coexistence/TripsWidget/PreviewSampleData.swift:15 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | Trips-Coexistence/TripsWidget/TripsWidget.swift:54 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | Trips-Coexistence/TripsWidget/TripsWidget.swift:54 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | Trips-SwiftData/Trips/ContentView.swift:152 |
@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
Trips-Coexistence/TripsWidget/PreviewSampleData.swift:14 — representative execution boundary
actor PreviewSampleData {
// ...
let schema = Schema([Trip.self, BucketListItem.self, LivingAccommodation.self])
// ...
}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. | Trips-Coexistence/Trips/AddBucketListItemView.swift:13 |
| State propagation | NotificationCenter |
NotificationCenter distributes named process-local events. | Trips-SwiftData/Trips/ContentView.swift:161 |
| State propagation | @Observable |
Observation macro publishes source-visible changes. | Trips-SwiftData/Trips/LocationSearchView.swift:11 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | Trips-Coexistence/Trips/AddBucketListItemView.swift:8 |
| Source import | SwiftData |
The cited file imports this module; runtime use and architectural role are not inferred. | Trips-Coexistence/Trips/Persistence.swift:9 |
| Source import | WidgetKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Trips-Coexistence/Trips/AddTripView.swift:9 |
| Source import | CoreData |
The cited file imports this module; runtime use and architectural role are not inferred. | Trips-Coexistence/Trips/BucketListView.swift:9 |
| Source import | MapKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Trips-SwiftData/Shared/Trip.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
Trips-Coexistence/Trips/TripsApp.swift:11 — representative type boundary
@main
struct TripsApp: App {
let persistenceController = PersistenceController.shared
// ...
}| 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
struct AddBucketListItemView: View {
// ...
@Environment(\.dismiss) private var dismiss
// ...
}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 | 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 isolation | Trips-Coexistence/TripsWidget/PreviewSampleData.swift:14 |
A declared actor creates an explicit isolation boundary; its executor is not described as a background thread. |
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 |
Cited implementation, TripsApp |
Trips-Coexistence/Trips/BucketListView.swift |
Cited implementation, CoreData, BucketListView, BucketListItemToggle |
Trips-Coexistence/Trips/AddBucketListItemView.swift |
Cited implementation, SwiftUI state property wrapper, SwiftUI, AddBucketListItemView |
Trips-SwiftData/Trips/MapCameraController.swift |
MapCameraController |
Trips-SwiftData/Trips/LocationSearchView.swift |
Cited implementation, @Observable, LocationSearchCompleter, LocationSearchView, LocationSearchSheet, CompletionLabel |
Trips-Coexistence/TripsWidget/PreviewSampleData.swift |
PreviewSampleData, actor, @MainActor |
Trips-Coexistence/TripsWidget/TripsWidget.swift |
Task closure isolated to MainActor, Task, TripsWidget, Provider, SimpleEntry, TripsWidgetEntryView |
Trips-SwiftData/Trips/ContentView.swift |
await suspension point, NotificationCenter, ContentView, Segment, SortOption, GroupOption |
Trips-Coexistence/Trips/Persistence.swift |
SwiftData, PersistenceController |
Trips-Coexistence/Trips/AddTripView.swift |
WidgetKit, AddTripView |
Trips-SwiftData/Shared/Trip.swift |
MapKit, Location, Trip, PersonalTrip, Reason, BusinessTrip |
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-CoreData/Trips/BucketListView.swift |
BucketListView, BucketListItemToggle |