Core Data in Swift
- 73Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What is Core Data, and what kind of programming model does it provide?
Core Data is an object-oriented database framework for creating, storing, fetching, and relating objects.
Or swipe the card
73 cards
What is Core Data, and what kind of programming model does it provide?
Core Data is an object-oriented database framework for creating, storing, fetching, and relating objects.
How does Core Data relate an object model to a relational database?
It maps objects and their variables to tables and rows in the underlying relational database.
Does an app need to issue SQL statements to use Core Data?
No. Core Data handles SQL storage behind the scenes, so the app can work with objects instead.
What does the Xcode Core Data model editor let you define?
Entities, their attributes, and graphical relationships between entities.
What does Xcode generate from a Core Data model?
Classes that represent the model’s entities and attributes.
How can developers add behavior to generated Core Data classes?
By adding extensions with methods and computed properties.
How can Core Data objects serve a SwiftUI interface?
They can provide the data displayed by the UI, replacing data stored in arrays or dictionaries in view models.
What major database capabilities does Core Data provide?
Creating objects, changing their values, establishing relationships, saving objects, and fetching objects that match criteria.
What are some advanced Core Data capabilities that were outside the introductory coverage?
Features such as optimistic locking and other advanced database functionality.
What is the recommended way to add Core Data when beginning a new app project?
Select the “Use Core Data” option when creating the Xcode project.
What does Xcode’s “Use Core Data” project option create?
A blank data model, persistent-store setup code, and code that makes a managed object context available to the app’s views.
What role does the persistent container play in the Core Data setup?
It holds the Core Data stack and provides access to the persistent database store.
What is the managed object context in Core Data?
It is the view or access point through which code creates, fetches, and works with database objects.
How does the app make the managed object context available to SwiftUI views?
The scene setup retrieves the context from the persistent container and places it in the SwiftUI environment.
Why must a managed object context be passed into a sheet or popover that uses Core Data?
A sheet or popover has its own environment, so it needs the context passed into that environment to perform fetches and access the database.
How can an existing app add Core Data using the setup method recommended in the lecture?
Create a new project with “Use Core Data” enabled, then move the existing source code into it.
What does enabling CloudKit alongside Core Data allow an app to do?
Mirror database contents to iCloud so users can access them across their devices.
Which entities were used in the flight-tracking data model?
Airport, Airline, and Flight.
What attributes were modeled for an Airline?
Code, full name, and short name; all are strings.
What is an Airport’s ICAO attribute used for?
It is a string identifier for the airport, such as KSFO or KSJC.
Which Airport attributes were stored as numbers, and what types were used?
Latitude and longitude were stored as Doubles.
What other descriptive attributes were modeled for an Airport?
Its location, time zone, and name.
What does a Flight’s ident represent?
A string that uniquely identifies a flight, such as UA475.
What attributes describe a Flight’s aircraft and schedule?
Aircraft is a string; arrival, departure, and filed departure are dates.
What is a Flight’s filed departure time?
The time the pilot filed as the intended departure time, which may exist before the flight has departed.
Why might a Flight have a filed departure time but no departure time?
It may still be on the ground, delayed, or not yet scheduled to depart.
Why are Core Data attributes often optional in the generated Swift code?
A database object may be incomplete or corrupted, so an attribute that is normally expected to have a value may be absent.
What relationships does a Flight have in the example model?
It has an origin Airport, a destination Airport, and an Airline.
What inverse relationships connect an Airport to Flights?
An Airport has flightsFrom and flightsTo relationships, representing flights departing from and arriving at it.
Why are an Airport’s flightsFrom and flightsTo relationships to-many?
Multiple flights can depart from or arrive at the same airport.
What relationship does an Airline have to Flights?
An Airline has a to-many relationship to the Flights it operates.
What collection type represents a to-many Core Data relationship in the generated model?
An Objective-C-style NSSet, rather than a native Swift Set.
What happens to the inverse relationship when a Flight’s origin is set to an Airport?
Core Data automatically adds the Flight to that Airport’s flightsFrom relationship.
Does a developer need to update both sides of a modeled Core Data relationship manually?
No. Core Data keeps the inverse sides of the relationship synchronized.
How can an extension present an NSSet relationship as a Swift Set of Flight objects?
Provide a computed property that converts the stored NSSet to Set and converts new values back when setting it.
Why might a computed relationship property return an empty Set when the stored relationship is nil?
It avoids exposing an optional collection and gives callers a safe, convenient empty collection instead.
What is an NSFetchRequest used for?
It describes what kind of Core Data objects to fetch and can specify criteria and sorting.
How is the entity type specified when creating an NSFetchRequest?
The request is generic over the object type and uses the entity’s name, such as Airport, Airline, or Flight.
What does an NSPredicate specify in a Core Data fetch?
Which database objects should match the request, based on criteria such as attribute values.
What does the predicate
icao_ = %@with an ICAO argument request?Airports whose stored ICAO attribute equals the supplied airport code.
What are NSSortDescriptors used for in a fetch request?
They specify which attributes to sort by and whether each sort is ascending or descending.
Why specify sort descriptors in a Core Data request rather than sort only after fetching?
The database can perform the sorting efficiently before returning the results.
How does code execute a Core Data fetch?
Call
context.fetch(request)using the managed object context.Why can a Core Data fetch or save require
try?Both operations can throw errors, for example if the database operation fails.
What does
try? context.fetch(request)return when the fetch fails?Nil.
What does a successful fetch that matches no objects return?
An empty array, which is different from nil indicating a fetch error when using
try?.How can a fetch for all objects be represented with an NSPredicate?
Use
TRUEPREDICATE, which always evaluates to true.How can an app avoid creating a duplicate Airport when given its ICAO code?
Fetch for an Airport with that ICAO first; return the existing object if found, and create one if not.
What does the
@ObservedObjectwrapper provide when a Core Data object is used in a SwiftUI view?It lets the view observe an individual object, such as a Flight, and use its properties in the UI.
Why might an observed Core Data object need an explicit
objectWillChange.send()call?An object’s changes may not automatically trigger the expected SwiftUI update, so explicitly sending the notification can invalidate dependent views.
What is
@FetchRequestin SwiftUI?A property wrapper that fetches Core Data objects and keeps the results synchronized with the database.
How is
@FetchRequestdifferent from a one-time fetch?It acts as a standing query that keeps updating its results as matching database objects are added, removed, or changed.
What is the type of the collection provided by a FetchRequest property?
FetchedResults for the requested object type.
Can FetchedResults be used with SwiftUI’s ForEach?
Yes. FetchedResults is a collection, so it can be iterated over with ForEach.
How can FetchedResults be converted to an Array or sorted?
Pass it to the Array initializer to create an array, or call a collection operation such as
sorted()to produce a sorted array.What happens to a SwiftUI view when a FetchRequest’s results change?
The view is invalidated and redrawn to reflect the current matching database objects.
How can a view initialize a FetchRequest using criteria passed into its initializer?
Initialize the property wrapper’s backing storage, such as
_flights, inside the view’s initializer.Why can a FetchRequest for a single object still be useful?
It continues tracking the database, so its results update if that object changes or stops matching the criteria.
How can a Core Data model’s optional attribute be exposed as a non-optional property in an extension?
Use a computed property that reads the stored optional attribute and supplies a suitable fallback or handles the missing-value error.
Why might an extension store the generated property under a name with a trailing underscore?
The underscored name distinguishes the raw database property from a computed property that provides a safer or more convenient API.
When writing a predicate or sort descriptor, should you use a computed Swift property or the stored database attribute name?
Use the stored database attribute name, because fetch predicates and sorting operate on database fields.
How does the flight-tracking workflow use data returned by FlightAware?
It fetches flight information, stores or updates it in Core Data, and builds the UI by reading from the database.
Why can storing fetched network data in Core Data simplify a UI?
The UI can consistently read from the database rather than managing incoming network results directly.
How can a newly requested Airport be represented before its full network information arrives?
Create it with the known ICAO code, return it, and fill in its remaining attributes asynchronously when FlightAware responds.
How can a Core Data object obtain the context it came from?
An object can access its
managedObjectContextproperty.Why can using an object’s own managed object context avoid passing context everywhere?
The object already knows the context from which it came, and that context can be used to create or fetch related objects.
How can a Flight’s string airport codes and airline code from a network response become Core Data relationships?
Look up or create the corresponding Airport and Airline objects in the current context, then assign them to the Flight’s relationship properties.
Why might incoming flight data update an existing Flight instead of always creating a new one?
Flights can be found by their identifier and updated as new information arrives.
How should an app handle errors when saving a context?
Use
do/catcharound the throwing save operation to inspect or handle the error; a failure may indicate a serious issue such as insufficient storage.What should happen to a Flight’s observers after a substantial update to that Flight?
Send
objectWillChangeso views observing the Flight can refresh.How can a FlightSearch build a predicate from optional origin and airline filters?
Start with the destination condition, then add origin and airline conditions and their arguments when those filters are present.
Why should a FetchRequest’s predicate include every active FlightSearch criterion?
If it includes only the destination, the results may ignore selected filters such as origin or airline.
What is the effect of changing the destination and then confirming the filter selection?
The app can fetch incoming flights for the newly selected destination.

Ask anything
We've read this whole study set, so questions about it get the best answers.