Persistence with Core Data
Core Data is Apple's framework for saving your app's objects on the device. You'll meet the NSManagedObjectContext , define entities , run fetch requests , drive a live list with @FetchRequest , and save changes.
Learn Persistence with Core Data in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ Insert and Save
You create objects inside a context , set their attributes, then call save() to persist them. Nothing reaches disk until you save.
2️⃣ Fetch Requests
An NSFetchRequest describes what to load. Attach an NSPredicate to filter and sort descriptors to order, then call context.fetch(request) .
Your turn. Fill in the request type and the fetch method.
3️⃣ @FetchRequest in SwiftUI
In SwiftUI, @FetchRequest loads managed objects and keeps the view in sync — add or delete an object and the list updates automatically.
Now you try. Fill in the delete and save methods.
📋 Quick Reference
Create a Task, save it, then fetch the unfinished ones with a predicate and print their names.
Practice quiz
What is Core Data primarily used for?
- Drawing UI
- Persisting and managing an object graph on the device
- Making network calls
- Animating views
Answer: Persisting and managing an object graph on the device. Core Data manages and persists a graph of model objects locally.
What does an NSManagedObjectContext represent?
- A scratchpad for creating, editing, and saving managed objects
- A network session
- A SwiftUI view
- A JSON decoder
Answer: A scratchpad for creating, editing, and saving managed objects. The context is the in-memory workspace where you make and track changes before saving.
In Core Data, an 'entity' is most like...
- A function
- A thread
- A table or class describing a kind of object
- A URL
Answer: A table or class describing a kind of object. An entity defines the structure (attributes) of a type of stored object.
Which type do you use to query for objects?
- URLRequest
- NSFetchRequest
- JSONDecoder
- DispatchQueue
Answer: NSFetchRequest. NSFetchRequest describes what objects to fetch and how to filter/sort them.
Which method actually fetches results from a context?
- context.save()
- context.delete()
- context.reset()
- context.fetch(request)
Answer: context.fetch(request). context.fetch(request) executes a fetch request and returns the matching objects.
How do you persist changes to disk?
- context.commit()
- context.save()
- context.flush()
- context.write()
Answer: context.save(). Calling save() on the context writes pending changes to the persistent store.
Which property wrapper loads and observes Core Data results in SwiftUI?
- @FetchRequest
- @State
- @Published
- @Binding
Answer: @FetchRequest. @FetchRequest fetches managed objects and keeps the view updated as data changes.
To refine which objects a fetch returns, you set its...
- timeout
- statusCode
- predicate (NSPredicate)
- scheme
Answer: predicate (NSPredicate). An NSPredicate on the fetch request filters which objects match.
How do you remove a managed object?
- context.delete(object)
- object.remove()
- context.drop()
- object = nil
Answer: context.delete(object). context.delete(object) marks it for deletion; save() makes it permanent.
An NSPersistentContainer mainly provides...
- A view hierarchy
- A network layer
- A JSON encoder
- The Core Data stack including the context and store
Answer: The Core Data stack including the context and store. NSPersistentContainer sets up and holds the Core Data stack for you.