The Model Container & Context
Set up storage with modelContainer.
The Model Container & Context is a free SwiftUI Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What a Container Is
The model container is the engine that sets up your storage on disk. Your whole app shares one container for all its SwiftData models.
Adding the Container
Attach a container to your app with the modelContainer modifier on a scene. One line gives every view access to your saved data.
WindowGroup {
ContentView()
}
.modelContainer(for: Task.self)Listing Multiple Models
Pass an array to register several models at once. The container then knows about every type your app needs to save.
.modelContainer(for: [Task.self, Project.self])What a Context Is
The model context is your working space for changes. You insert, edit, and delete through it, then it saves to the container.
Reading the Context
Grab the active context inside a view with the @Environment property wrapper. SwiftData injects it for you automatically.
@Environment(\.modelContext) private var contextInserting an Object
Call insert on the context to start tracking a new object. From this point SwiftData watches it for saving.
context.insert(Task(title: "Buy milk"))Automatic Saving
SwiftData usually autosaves changes for you at smart moments. You rarely need to save by hand in a simple app.
Saving Manually
When you need certainty, call save on the context to write changes right now. It is handy before a critical step.
try context.save()In-Memory Containers
Set isStoredInMemoryOnly to true for a throwaway store. It is perfect for previews and tests that should not touch the disk.
let config = ModelConfiguration(
isStoredInMemoryOnly: true)Custom Configuration
A ModelConfiguration lets you tune where and how data is stored. You pass it when building a container by hand.
let container = try ModelContainer(
for: Task.self, configurations: config)One Container, Many Views
Because the container lives at the app level, every view shares the same data. Edits in one screen show up everywhere instantly.
Quick Check
Quick check on container and context.
Recap: Container & Context
You set up storage with modelContainer, then insert and save through the context from @Environment. Your app can now read and write data. ✅
Frequently asked questions
Is the “The Model Container & Context” lesson free?
Yes — the full text of “The Model Container & Context” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Model Container & Context”?
Set up storage with modelContainer. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Model Container & Context” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining @Model Types
- The Model Container & Context
- Querying with @Query
- Inserting, Updating & Deleting