Why SwiftData
Hub › iOS › Advanced › Why SwiftData
Goal
Understand why local persistence matters and how SwiftData compares to Core Data, then add a local "favorite" feature to the intermediate list app.
Prerequisites
- Intermediate MVVM list app (URLSession + async/await)
The persistence gap
The intermediate app fetches data from a public API every launch. This means:
- No offline access — the screen is blank without network.
- Slow start — users stare at a spinner before seeing content.
- No user data — bookmarks, favorites, or notes can't be saved.
Local persistence fixes all three. You cache API results and store user-created data in a database on the device.
SwiftData vs Core Data
| Feature | Core Data | SwiftData |
|---|---|---|
| API style | Objective-C heritage, NSManagedObject | Swift-native with @Model macro |
| Boilerplate | NSPersistentContainer, entity descriptions, codegen | ModelContainer + @Model — zero config |
| Concurrency | Manual viewContext / performBackgroundTask | @MainActor by default, ModelActor for background |
| Observable | @ObservedObject for fetch results | @Query auto-refreshes the view |
| Migration | Lightweight + heavy (versioning) | Automatic lightweight |
SwiftData is not a wrapper around Core Data — it's a separate framework built on the same SQLite storage engine, but with a modern Swift-first API.
Plan for this tier
Over the next pages you will:
- Add SwiftData to the project (this page)
- Create
@Modelclasses for cached items and user favorites - Replace the intermediate
@ObservableObjectViewModel with a SwiftData-backed one - Favorites persist across launches and sync with the list view
Add SwiftData to the project
In your intermediate Xcode project, add the import:
import SwiftDataEvery SwiftData app needs a ModelContainer in the app entry point:
import SwiftUI
import SwiftData
@main
struct ListApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: [CachedItem.self])
}
}The .modelContainer modifier creates the container and injects it into the environment. The CachedItem model is defined next.
Next: Add SwiftData — define @Model classes and build a CRUD ViewModel.