Creating & Saving Entities
Insert records and persist the context.
Creating & Saving Entities is a free SwiftUI Academy lesson on CoddyKit — lesson 3 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.
Adding New Data
Reading is half the story. To grow your app you also need to create new records and store them safely.
You Need the Context
Every new object is born inside a managed object context, so grab the context from the environment first.
@Environment(\.managedObjectContext) var contextMaking an Instance
Create an entity by initialising it with the context, which registers the new object for tracking.
let task = Task(context: context)Setting Attributes
Assign values to the object's attributes just like setting properties on any Swift object.
task.title = "Buy milk"
task.isDone = falseNothing Is Saved Yet
At this point the record only lives in memory. It becomes permanent when you save the context.
Calling save()
Persist all pending changes at once with a single save call, wrapped in a do-catch for safety.
do {
try context.save()
} catch {
print("Save failed: \(error)")
}Saving Is Transactional
One save writes every pending insert, edit, and delete together, so your store never lands half-updated.
Updating Existing Records
To edit, change the object's attributes and call save again. Core Data tracks what is dirty for you.
Deleting a Record
Remove an object with delete on the context, then save to make the removal stick.
context.delete(task)
try? context.save()Why Catch Errors
Saving can fail, for example if a required value is missing, so handling the error keeps your app from crashing.
Save After Each Action
A common habit is to save right after the user adds, edits, or deletes, so work is never lost on a sudden close.
Quick Check
You created and stored a record. One quick question about the flow.
Recap
You learned to create an entity with the context, set its attributes, and call save to insert, update, or delete records. ✨
Frequently asked questions
Is the “Creating & Saving Entities” lesson free?
Yes — the full text of “Creating & Saving Entities” 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 “Creating & Saving Entities”?
Insert records and persist the context. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Creating & Saving Entities” 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
- The Core Data Stack
- Fetching with @FetchRequest
- Creating & Saving Entities
- Predicates & Sort Descriptors