In SwiftData, you mark your data model classes with @Model. What does this attribute do?
Think about what you want SwiftData to do with your data classes.
The @Model attribute tells SwiftData to treat the class as a persistent model. This enables automatic saving, fetching, and syncing of data.
You have a SwiftUI view that needs to save data using SwiftData, but you forget to add @Environment(\.modelContext). What will happen when you try to save?
Think about how SwiftUI provides the context to views.
If you don't inject modelContext, the view cannot access the persistence context. This causes a runtime crash because the environment key is missing.
In a SwiftUI app using SwiftData, when is the ModelContainer typically created and injected?
Consider where you set up global app state in SwiftUI.
The ModelContainer is created once in the app's main struct (e.g., in @main) and injected into the environment for all views to access.
You want to navigate from a list view to a detail view showing a SwiftData model object. How should you pass the object?
Think about how SwiftData manages object identity and sharing.
Passing the model object directly keeps the same managed instance, allowing edits and updates to sync automatically.
Consider this code snippet inside a SwiftUI view:
func saveItem() {
let newItem = Item(name: "Test")
modelContext.insert(newItem)
// Missing save call here
}Why does the new item not persist after app restart?
Think about what final step is needed to persist changes.
In SwiftData, inserting an object adds it to the context, but you must call modelContext.save() to write changes to disk.