LimitedRound 2 is open. Get your first month free, no extra charge.Join the waitlist ›
Wire in your backend

How to use Core Data in SwiftUI

TL;DR. Core Data is Apple's framework for storing structured data on device. In SwiftUI you put the managed object context in the environment and read records with @FetchRequest, which updates the view automatically. SwiftData is the newer alternative for apps targeting recent iOS versions.

What Core Data does for an iOS app

Core Data manages your app's local data: the entities, their fields, the relationships between them, and saving and loading from disk. It is how an offline-first app remembers a user's notes, habits, or logged workouts without a server. It typically stores data in a SQLite file but exposes objects and relationships rather than SQL. For SwiftUI, the value is the @FetchRequest property wrapper, which fetches records and re-renders the view automatically when they change.

Adding it with AppFlight, and manually

In AppFlight you describe the data the app needs to remember, and the AI scaffolds the model and the SwiftUI views that read and write it, using SwiftData for new apps on recent iOS versions or Core Data when the model or iOS target calls for it. This is fully local, so there are no keys to manage. If you also want the data on a server or shared across users, AppFlight connects Supabase as a separate backend.

Manually, you define an entity in a data model, set up an NSPersistentContainer, inject its viewContext into the SwiftUI environment, fetch with @FetchRequest, and call context.save() after changes.

A SwiftUI example

Read and add records with the context from the environment:

import CoreData
import SwiftUI

struct TaskListView: View {
    @Environment(\.managedObjectContext) private var context
    @FetchRequest(sortDescriptors: [SortDescriptor(\Task.createdAt)])
    private var tasks: FetchedResults<Task>

    var body: some View {
        List(tasks) { task in
            Text(task.title ?? "")
        }
        .toolbar {
            Button("Add") {
                let task = Task(context: context)
                task.title = "New task"
                task.createdAt = Date()
                try? context.save()
            }
        }
    }
}

Alternatives

SwiftData is the modern, Swift-first framework that uses macros and a cleaner API, and it is the better default for new apps on recent iOS versions. Core Data remains the right choice for complex object graphs, fine-grained migrations, and support on older iOS versions. For data that must live on a server or sync across users, Core Data is the wrong tool, and a backend like Supabase or Firebase is what you want. To sync local Core Data through a user's own iCloud, use NSPersistentCloudKitContainer.

FAQ

Should I use Core Data or SwiftData?

SwiftData is the modern, Swift-first framework and is simpler for new apps that target recent iOS versions. Core Data is older, more capable for complex models, and supported on older iOS versions. For a brand new SwiftUI app on a recent iOS, SwiftData is usually the better default.

Does Core Data sync across devices?

Not by itself. Pair it with NSPersistentCloudKitContainer to sync through the user's iCloud account. For a shared backend across users, you need a server database such as Supabase, not Core Data.

Is Core Data a database?

Core Data is an object graph and persistence framework that usually stores data in a local SQLite file. You work with objects and relationships, not raw SQL. It runs entirely on device.

Sources

Build this app without opening Xcode.

AppFlight turns a plain-English prompt into a real native iOS app and ships it to the App Store. Round 2 is open: free for your first month.

Join the waitlist