Core Data in Swift
- 52Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
How does Core Data let a Swift app work with a relational database without requiring the developer to write SQL for ordinary object operations?
How does Core Data let a Swift app work with a relational database without requiring the developer to write SQL for ordinary object operations?
52 questions
- 1
How does Core Data let a Swift app work with a relational database without requiring the developer to write SQL for ordinary object operations?
- A
It requires SQL for storage but translates only SQL query results into Swift objects
- B
It maps objects and their properties to database structures, then exposes them through an object-oriented API
- C
It converts every database table into a SwiftUI view and performs storage through view updates
- D
It replaces the database with in-memory Swift collections that are serialized manually
Correct answer
BIt maps objects and their properties to database structures, then exposes them through an object-oriented API
Explanation
Core Data maps objects and their properties to relational database structures, while developers typically create, retrieve, and change data through objects and Core Data APIs rather than writing SQL statements.
- A
- 2
In a Core Data model, what does an entity represent most directly?
- A
A kind of object whose instances have defined attributes and relationships
- B
A database query that determines which records are deleted
- C
A single row that stores every property for the entire application
- D
A SwiftUI screen that displays one database table
Correct answer
AA kind of object whose instances have defined attributes and relationships
Explanation
An entity defines a kind of object, such as Flight or Airport, along with its attributes and relationships. Core Data uses the model to generate the corresponding object-supporting code.
- A
- 3
What role does the Core Data model editor play in an app?
- A
It stores runtime data directly in the project file instead of in a database
- B
It defines entities, attributes, and relationships that Core Data uses to generate object classes
- C
It replaces extensions by placing all application behavior in the model editor
- D
It writes the app's SwiftUI layouts and generates database queries from view hierarchies
Correct answer
BIt defines entities, attributes, and relationships that Core Data uses to generate object classes
Explanation
The model editor defines the objects, their attributes, and their relationships. Xcode uses this model to generate the classes that represent those objects in code.
- A
- 4
Why might a developer add extensions to Core Data-generated classes?
- A
To add custom methods and computed properties while keeping generated storage properties in the model
- B
To replace the persistent store with a new SQL database for each object
- C
To turn every Core Data object into a value type automatically
- D
To make the generated attributes inaccessible to all application code
Correct answer
ATo add custom methods and computed properties while keeping generated storage properties in the model
Explanation
Extensions can add behavior such as methods and computed properties to generated classes. The stored properties remain associated with the Core Data model and database.
- A
- 5
How can Core Data objects contribute to a SwiftUI app's displayed data?
- A
Views must copy all object properties into SQL statements before displaying them
- B
Views can access the objects as their data source rather than relying only on arrays held in view models
- C
Views can display the objects only after converting them into UserDefaults entries
- D
Views use Core Data objects only to define layout and not to provide displayed values
Correct answer
BViews can access the objects as their data source rather than relying only on arrays held in view models
Explanation
Core Data objects can serve as the source of data for UI elements. This can replace an approach where arrays or dictionaries in view models are the main source of displayed data.
- A
- 6
Which set of capabilities is described as part of Core Data's basic object-oriented use?
- A
Compressing files, synchronizing network requests, and generating user interface code
- B
Creating objects, changing their properties, establishing relationships, saving, and fetching by criteria
- C
Rendering views, handling gestures, and choosing animations for stored objects
- D
Creating SQL tables manually, writing joins, and managing every database row directly
Correct answer
BCreating objects, changing their properties, establishing relationships, saving, and fetching by criteria
Explanation
Core Data supports creating objects, changing their properties, relating objects, saving them, and fetching objects that meet criteria. The lecture also notes that Core Data has additional database features beyond this introductory scope.
- A
- 7
What is the role of the managed object context in Core Data?
- A
It provides the app with a way to access and work with objects in a particular database store
- B
It defines the attributes and relationships before the object model is created
- C
It stores network responses temporarily without connecting to the persistent store
- D
It is a generated SwiftUI view that presents all database tables in alphabetical order
Correct answer
AIt provides the app with a way to access and work with objects in a particular database store
Explanation
The managed object context is the app's window onto the database. Operations such as creating objects, fetching them, and saving changes use a context.
- A
- 8
In the described project setup, why is the managed object context passed through SwiftUI's environment?
- A
So every view can create its own independent persistent store automatically
- B
So views can avoid importing Core Data in files that use Core Data types
- C
So SwiftUI can convert database relationships into navigation links
- D
So views can access the database context for Core Data operations such as fetching and creating objects
Correct answer
DSo views can access the database context for Core Data operations such as fetching and creating objects
Explanation
Passing the context through the environment makes it available to SwiftUI views that need to access the database. A view that performs Core Data work needs access to the appropriate context.
- A
- 9
Which model best describes a Flight-to-Airport origin relationship in the example?
- A
A Flight has one origin Airport, while an Airport can have many flights originating from it
- B
A Flight has many origin Airports, while each Airport can originate only one Flight
- C
A Flight and an Airport must always be stored as the same entity
- D
A Flight stores the Airport's screen layout instead of a relationship to an Airport object
Correct answer
AA Flight has one origin Airport, while an Airport can have many flights originating from it
Explanation
The Flight's origin is one Airport object. The inverse relationship on Airport, flightsFrom, is to-many because many flights can originate from the same airport.
- A
- 10
Why is an Airport's flightsTo relationship modeled as to-many?
- A
Because every Airport must contain a separate database for each destination
- B
Because an Airport can be related only to Flights that have not departed
- C
Because each Flight must have several destination Airports
- D
Because multiple Flight objects can have the same destination Airport
Correct answer
DBecause multiple Flight objects can have the same destination Airport
Explanation
An airport can be the destination for many flights, so its flightsTo relationship is to-many. The corresponding destination relationship on each Flight refers to one Airport.
- A
- 11
What happens to an inverse relationship when a Flight's origin is set to an Airport using a properly configured Core Data relationship?
- A
Core Data updates the Airport's corresponding flightsFrom relationship as well
- B
The inverse relationship is updated only after the app restarts
- C
The developer must manually create a second Airport object for the inverse side
- D
The Flight's origin is converted into a string and the inverse is discarded
Correct answer
ACore Data updates the Airport's corresponding flightsFrom relationship as well
Explanation
Core Data maintains both sides of a configured relationship. Setting a Flight's origin can automatically add that Flight to the Airport's flightsFrom relationship, and setting the inverse side works correspondingly.
- A
- 12
In the example data model, what is the type of a Flight's destination property conceptually?
- A
An array of database contexts
- B
A set of Airport objects
- C
A single Airport object
- D
A string containing the airport's name
Correct answer
CA single Airport object
Explanation
Each Flight has one destination Airport, so destination is a relationship to a single Airport object. The Airport's inverse flightsTo relationship is the to-many side.
- A
- 13
Which description correctly matches the example's core entity attributes?
- A
Airline has origin and destination airports; Airport has flight identifiers; Flight has an airport time zone
- B
Airline has arrival dates; Airport has airline short names; Flight has only latitude and longitude
- C
Airline has latitude and longitude; Airport has aircraft and departure dates; Flight has only an airline code
- D
Airline has code and names; Airport has an ICAO code and location-related values; Flight has an identifier, aircraft, and date values
Correct answer
DAirline has code and names; Airport has an ICAO code and location-related values; Flight has an identifier, aircraft, and date values
Explanation
The example models Airline with code, full name, and short name; Airport with ICAO, latitude, longitude, location, time zone, and name; and Flight with ident, aircraft, arrival, departure, and filed-departure values.
- A
- 14
Why does the example include a filed departure date in addition to a departure date?
- A
The filed departure date is the local time zone used to convert every airport's latitude
- B
The filed departure date identifies which airline owns the airport
- C
A flight may have a filed departure time even when it has not yet departed, such as when delayed or not yet scheduled to depart
- D
The filed departure date is a duplicate of arrival used only for sorting
Correct answer
CA flight may have a filed departure time even when it has not yet departed, such as when delayed or not yet scheduled to depart
Explanation
The filed departure time represents a time the pilot has filed for departure. A flight may still be on the ground, delayed, or not yet scheduled to leave, so this can differ from an actual departure time.
- A
- 15
What does an Optional setting in the Core Data model editor indicate in the example, and what does it not mean?
- A
It permits the database attribute to be absent; it is not the same setting as Swift's Optional type declaration
- B
It indicates the relationship is to-many; it means the property must be a Swift array
- C
It marks a property as computed; it means the value is never stored in the database
- D
It requires the Swift property to be non-Optional; it is the same as a non-nil assertion
Correct answer
AIt permits the database attribute to be absent; it is not the same setting as Swift's Optional type declaration
Explanation
The model editor's Optional setting concerns whether a value may be absent in the database. The lecture emphasizes that this setting is distinct from Swift's Optional type, even though generated properties are Optional in the example.
- A
- 16
Why does the example wrap some generated Core Data properties in computed properties?
- A
To convert computed values into SQL statements before each view redraw
- B
To expose safer or more convenient Swift types, such as a non-Optional value or a Swift Set instead of an optional NSSet
- C
To ensure each fetch request returns a single database row
- D
To prevent Core Data from maintaining inverse relationships
Correct answer
BTo expose safer or more convenient Swift types, such as a non-Optional value or a Swift Set instead of an optional NSSet
Explanation
Computed wrappers can make generated storage properties more convenient to use. The example converts Objective-C-style optional NSSet relationships into Swift Sets and provides non-Optional accessors for values expected to be present.
- A
- 17
Why can a Core Data relationship to-many property appear as an NSSet rather than a Swift Set of a specific class?
- A
The model editor converts every to-many relationship into a database context
- B
The relationship is actually a string that contains multiple object identifiers
- C
The generated relationship uses an Objective-C-style collection that may not express the contained Swift object type directly
- D
Core Data represents every to-many relationship as a sorted Swift array
Correct answer
CThe generated relationship uses an Objective-C-style collection that may not express the contained Swift object type directly
Explanation
In the example, to-many relationships are represented by Objective-C-style NSSet values, which can contain Any. A computed property can convert the value to a Swift Set of the expected type.
- A
- 18
What does the example's computed wrapper return when an optional to-many relationship has no stored value?
- A
An empty Swift Set, giving callers a non-Optional collection to work with
- B
A newly created Flight object unrelated to the relationship
- C
A nil value, forcing every caller to unwrap the collection
- D
A database error that prevents the context from being saved
Correct answer
AAn empty Swift Set, giving callers a non-Optional collection to work with
Explanation
The computed wrapper can convert the underlying optional NSSet to a Swift Set and use an empty Set when the underlying value is absent. This avoids requiring callers to handle nil for that collection.
- A
- 19
Why might an identifier such as an Airport's ICAO code be exposed through a non-Optional computed property?
- A
Core Data guarantees that all database properties are automatically non-Optional
- B
An ICAO code is a relationship and therefore cannot have a missing value
- C
The app expects the identifier to be present for a valid Airport, so the wrapper can simplify access while treating absence as an error condition
- D
The identifier is calculated from the airport's latitude and longitude on every access
Correct answer
CThe app expects the identifier to be present for a valid Airport, so the wrapper can simplify access while treating absence as an error condition
Explanation
The example treats an Airport's ICAO code as a value that should always be set. A computed property can expose it as non-Optional, although the lecture notes that a missing value would be an error condition that production code might handle more carefully.
- A
- 20
When creating a new Core Data object in code, why is a managed object context supplied?
- A
The context makes the new object independent of the persistent store
- B
The context identifies the database environment in which the new object should be created
- C
The context automatically downloads all required attributes from a remote service
- D
The context supplies the object's display name and SwiftUI layout
Correct answer
BThe context identifies the database environment in which the new object should be created
Explanation
A context is required to create an object in the intended Core Data store. It is the access point used for database operations, including creation.
- A
- 21
Why can saving a managed object context require error handling?
- A
Saving fails whenever a fetched request returns an empty collection
- B
Saving can fail only when an object has a to-many relationship
- C
Saving always requires a network connection to FlightAware
- D
Saving can fail, for example if storage is unavailable or another database error occurs
Correct answer
DSaving can fail, for example if storage is unavailable or another database error occurs
Explanation
The context's save operation can throw because a failure such as a full disk or another storage problem is possible. Even if failure is unlikely, code must account for the throwing operation.
- A
- 22
What does an NSFetchRequest specify at a basic level?
- A
The kind of entity to fetch and, through its criteria, which matching objects are wanted
- B
The SwiftUI view to construct and the animations to apply to its rows
- C
The network endpoint to call and the response format to decode
- D
The database file to delete and the values to replace it with
Correct answer
AThe kind of entity to fetch and, through its criteria, which matching objects are wanted
Explanation
An NSFetchRequest describes what kind of objects to retrieve and can specify criteria for selecting them. Its entity identifies the object type, while its predicate and sort descriptors further define the request.
- A
- 23
What is the role of an NSPredicate in a Core Data fetch request?
- A
It specifies criteria that determine which objects should match the request
- B
It converts a managed object into a SwiftUI view
- C
It defines the order in which matched objects are stored permanently
- D
It creates a relationship between two entity types
Correct answer
AIt specifies criteria that determine which objects should match the request
Explanation
A predicate expresses the conditions objects must meet to be returned. For example, a request can ask for flights whose arrival is before the current time and whose origin is a particular airport.
- A
- 24
What information does an NSSortDescriptor provide for a fetch request?
- A
A property to sort by and whether the order should be ascending or descending
- B
A relationship to create and the context in which to create it
- C
A predicate to evaluate and a network service to contact
- D
An entity to fetch and whether its properties should be Optional
Correct answer
AA property to sort by and whether the order should be ascending or descending
Explanation
A sort descriptor specifies the property used for sorting and the direction of the order. Multiple descriptors can be used for primary and secondary sorting.
- A
- 25
Why does a context fetch return an Array in the example rather than a Set?
- A
Core Data can return only one object from a fetch request
- B
An Array is required because predicates cannot be applied to collections
- C
A Set cannot contain managed objects under any circumstances
- D
The fetched results are ordered according to the request's sort descriptors
Correct answer
DThe fetched results are ordered according to the request's sort descriptors
Explanation
The fetch is returned as an Array, and sort descriptors determine its ordering. Sorting can be performed efficiently by the database rather than after objects reach the app.
- A
- 26
When using try? with a fetch, what does the result distinguish?
- A
An empty Array indicates that the request was never sent to the context
- B
Nil indicates a fetch error, while an empty Array can indicate a successful fetch with no matches
- C
Nil indicates no matching objects, while an empty Array indicates a database error
- D
Nil indicates a successful fetch, while a nonempty Array indicates a saving error
Correct answer
BNil indicates a fetch error, while an empty Array can indicate a successful fetch with no matches
Explanation
With try?, a failed fetch produces nil. If the fetch succeeds but no objects match the predicate, it produces an empty Array; otherwise it returns an Array of matching objects.
- A
- 27
Why can a Core Data object use its managedObjectContext for later database operations?
- A
Every Core Data object creates a new persistent store when accessed
- B
The context is inferred from the object's SwiftUI parent view
- C
Objects retrieved from Core Data know the context from which they came
- D
Objects retain a context only if it is re-created from a predicate
Correct answer
CObjects retrieved from Core Data know the context from which they came
Explanation
The example notes that managed objects know the context they came from. An instance method on such an object can use that context for further operations without always receiving it as an argument.
- A
- 28
In a predicate such as "icao = %@", what is the purpose of the placeholder and supplied argument?
- A
The placeholder creates a new entity and the argument names its context
- B
The placeholder marks where a value, such as the requested ICAO code, is inserted into the predicate condition
- C
The placeholder represents a SwiftUI binding and the argument supplies a view
- D
The placeholder names a sort descriptor and the argument chooses ascending order
Correct answer
BThe placeholder marks where a value, such as the requested ICAO code, is inserted into the predicate condition
Explanation
Predicate format strings use placeholders for values. In the example, the predicate compares the database's ICAO field with the ICAO value supplied as an argument.
- A
- 29
Which predicate describes flights that have already arrived from a specified airport?
- A
Departure equals the current date and airline is absent
- B
Filed departure is earlier than the current date and destination is absent
- C
Arrival is earlier than the current date and origin equals the specified Airport
- D
Arrival is later than the current date and destination equals the specified Airport
Correct answer
CArrival is earlier than the current date and origin equals the specified Airport
Explanation
The example predicate selects flights whose arrival is before the current time and whose origin equals the specified airport. This describes flights that have arrived and came from that airport.
- A
- 30
What does a TRUEPREDICATE condition accomplish in the example?
- A
It matches no objects, allowing a fetch to return an empty collection
- B
It sorts all objects in descending order by their first property
- C
It matches every object, allowing a fetch to retrieve all objects of the requested entity
- D
It creates every possible relationship between the requested entities
Correct answer
CIt matches every object, allowing a fetch to retrieve all objects of the requested entity
Explanation
TRUEPREDICATE evaluates to true for every object, so it can be used to fetch all objects of the specified entity. The example also mentions a helper that returns this predicate.
- A
- 31
How can a predicate be built programmatically when some search criteria are optional?
- A
Replace missing criteria with empty database entities and fetch those entities
- B
Sort the objects first and use the sort descriptors as predicate arguments
- C
Start with a required condition, then add conditions and corresponding arguments only for criteria that are present
- D
Create a separate persistent store for every optional criterion and merge the results
Correct answer
CStart with a required condition, then add conditions and corresponding arguments only for criteria that are present
Explanation
The example builds a predicate format and argument list incrementally: it always includes the destination, then adds origin or airline conditions when those values are present.
- A
- 32
Why can an argument list for a predicate combining destination, origin, and airline be typed as [NSManagedObject]?
- A
NSManagedObject is the only class that can represent date and string values in a predicate
- B
The array stores SQL rows rather than the related Core Data objects
- C
A predicate requires all of its arguments to be identical objects
- D
Flight, Airport, and Airline inherit from NSManagedObject, allowing objects of those classes in the same array
Correct answer
DFlight, Airport, and Airline inherit from NSManagedObject, allowing objects of those classes in the same array
Explanation
NSManagedObject is the superclass of the generated Flight, Airport, and Airline classes in the example. An array of that superclass can therefore hold objects of those different subclasses as predicate arguments.
- A
- 33
Why does the example provide common fetch-request helpers for entities such as Airport and Flight?
- A
They reduce repeated setup by supplying the entity name and commonly used sort descriptors
- B
They convert all fetches into one-time SwiftUI state values
- C
They prevent callers from supplying predicates for those entities
- D
They store network responses in the model editor rather than the database
Correct answer
AThey reduce repeated setup by supplying the entity name and commonly used sort descriptors
Explanation
The helper methods create a fetch request with the appropriate entity and usual sorting, letting callers provide a predicate without repeating the same setup each time.
- A
- 34
If an Airport fetch request sorts by location and an Airline fetch request sorts by name, where does that sorting occur in the described approach?
- A
The database can perform the sorting using the request's sort descriptors
- B
The network service sorts the objects before Core Data receives them
- C
The SwiftUI view must sort each rendered row independently
- D
The model editor sorts the entities when the app is built
Correct answer
AThe database can perform the sorting using the request's sort descriptors
Explanation
Sort descriptors let the database perform sorting efficiently as part of the fetch. The returned Array is ordered according to those descriptors.
- A
- 35
What does a FetchRequest property wrapper provide to a SwiftUI view?
- A
A one-time Array that never changes after the view is initialized
- B
A FetchedResults collection kept current with objects matching the request
- C
A managed object context that is separate from the app's environment
- D
A SQL editor embedded inside the view's body
Correct answer
BA FetchedResults collection kept current with objects matching the request
Explanation
The @FetchRequest property wrapper exposes matching objects as FetchedResults and keeps the results up to date as the database changes.
- A
- 36
How does FetchedResults relate to an Array?
- A
FetchedResults is a Collection that can be used with collection-based APIs, but it is not itself an Array
- B
FetchedResults is an Array whose contents cannot be iterated with ForEach
- C
FetchedResults is a single managed object that must be converted into a collection
- D
FetchedResults is a predicate format string rather than a collection
Correct answer
AFetchedResults is a Collection that can be used with collection-based APIs, but it is not itself an Array
Explanation
FetchedResults conforms to Collection, so it can be used with APIs such as ForEach. It is not an Array, though it can be converted to one with Array's initializer.
- A
- 37
Why is @FetchRequest described as a standing query rather than a one-time fetch?
- A
It continues to track matching database objects as they are added or changed
- B
It queries the database once and stores a permanent copy in the view
- C
It continuously creates new objects even when no objects match
- D
It reruns only when the user explicitly taps a refresh button
Correct answer
AIt continues to track matching database objects as they are added or changed
Explanation
A FetchRequest property wrapper keeps tracking objects that match its criteria. If objects are added or changed so that they match, the results update and SwiftUI can refresh the UI.
- A
- 38
What can happen to a SwiftUI view when an object begins matching its @FetchRequest predicate?
- A
The object's relationships are converted into sort descriptors
- B
The persistent store is replaced with a new database file
- C
The fetched results update, which can invalidate and redraw the view
- D
The view is removed because the request has already completed
Correct answer
CThe fetched results update, which can invalidate and redraw the view
Explanation
When database changes affect which objects match the request, the fetched results update. SwiftUI then invalidates and redraws the view to reflect the current results.
- A
- 39
What is the purpose of @ObservedObject when a view receives an individual Core Data Flight?
- A
It automatically sorts all Flights by their arrival times
- B
It creates a new persistent container for the Flight
- C
It turns the Flight into a FetchRequest for every object in the database
- D
It lets the view observe that object and use its properties, such as the flight identifier
Correct answer
DIt lets the view observe that object and use its properties, such as the flight identifier
Explanation
A Core Data object can be used as an ObservedObject in a view, which can then display its properties. The lecture notes that object changes may require explicitly sending objectWillChange.
- A
- 40
What should the example do after making a wholesale update to a Core Data object when relying on ObservedObject-based view updates?
- A
Create a new model entity so the view has a different object to observe
- B
Replace the managed object context with a new persistent container
- C
Send objectWillChange so observing views can be notified of the change
- D
Convert the object's properties into a sort descriptor
Correct answer
CSend objectWillChange so observing views can be notified of the change
Explanation
The lecture notes that Core Data objects used as ObservedObjects do not appear to fire change notifications automatically in the described cases. Explicitly sending objectWillChange can tell observing views to update.
- A
- 41
Why can a FetchRequest be useful even when the app needs only one particular Flight?
- A
A FetchRequest is required to turn every Flight into a value type
- B
A single-object FetchRequest avoids using the managed object context
- C
A FetchRequest always returns exactly one object regardless of its predicate
- D
A predicate can select that Flight, and the request can still track changes that affect its match
Correct answer
DA predicate can select that Flight, and the request can still track changes that affect its match
Explanation
A FetchRequest can use a specific identifier to select one object. Because it tracks the database, it can update when that object's stored values change in a way relevant to the request.
- A
- 42
Why might a sheet that uses Core Data need the managed object context passed into its environment?
- A
A sheet's environment can contain only sort descriptors, not managed objects
- B
A sheet automatically changes the entity names used by every fetch request
- C
A sheet has its own environment boundary, and its fetches need access to the database context
- D
A sheet cannot display any ObservedObject unless it creates a new database
Correct answer
CA sheet has its own environment boundary, and its fetches need access to the database context
Explanation
The lecture explains that a sheet or popover has a separate environment context. The managed object context should be passed into that environment so that fetch requests in the presented view can access the database.
- A
- 43
How can a view initialize a @FetchRequest when its predicate depends on a value passed into the view?
- A
Set the predicate after each row has been rendered by ForEach
- B
Pass the predicate to objectWillChange instead of to the request
- C
Initialize the backing property-wrapper storage in the view's initializer using a configured FetchRequest
- D
Create a second persistent container whose entity name includes the predicate
Correct answer
CInitialize the backing property-wrapper storage in the view's initializer using a configured FetchRequest
Explanation
When the request depends on an input such as a search object, the view can initialize the backing storage (for example, _flights) in its initializer with the configured FetchRequest.
- A
- 44
Which approach best describes the data-flow pattern used in the Enroute example?
- A
Fetch database results once, then ignore all later updates from the external service
- B
Fetch external flight information, store or update it in Core Data, and build the UI by observing database results
- C
Display network responses directly and use Core Data only to store screen layouts
- D
Use arrays from a view model as the permanent store and copy them to Core Data only at shutdown
Correct answer
BFetch external flight information, store or update it in Core Data, and build the UI by observing database results
Explanation
The example shifts from displaying FlightAware responses directly to storing them in Core Data and building the UI from the database. This simplifies the UI's data flow and keeps it aligned with stored data.
- A
- 45
Why does the example look up an Airport by ICAO before creating a new one?
- A
A fetch request is required to establish the persistent store coordinator
- B
The ICAO code is generated only after an Airport object is saved
- C
The Airport may already exist, and reusing it avoids creating duplicate objects for the same identifier
- D
An Airport cannot be created until its latitude and longitude have been fetched
Correct answer
CThe Airport may already exist, and reusing it avoids creating duplicate objects for the same identifier
Explanation
The lookup checks whether an Airport with that ICAO is already in the database. If found, the existing object is returned; if not, the example creates one and fetches additional information.
- A
- 46
What can a newly created Airport contain before its asynchronous FlightAware information arrives?
- A
Only a network response, because its ICAO cannot be set locally
- B
All attributes and relationships, because the fetch is synchronous
- C
Its known ICAO code, while other values such as coordinates may still be absent
- D
A complete set of incoming Flights, but no identifier
Correct answer
CIts known ICAO code, while other values such as coordinates may still be absent
Explanation
The example creates an Airport as soon as its ICAO is known, then fills in other information later when the asynchronous response arrives. Code using the object must tolerate missing values during that interval.
- A
- 47
After airport information returns asynchronously, what does the example's update operation do?
- A
Deletes all related Flights before updating the Airport's name
- B
Finds the Airport by its ICAO, updates its stored fields, saves the context, and notifies observers
- C
Converts the response into sort descriptors without changing database values
- D
Creates a second Airport for every response and discards the first object
Correct answer
BFinds the Airport by its ICAO, updates its stored fields, saves the context, and notifies observers
Explanation
The update operation uses the returned ICAO to find the previously created Airport, sets values such as latitude and longitude, saves the context, and sends change notifications where needed.
- A
- 48
Why can an Airport instance method use its managedObjectContext to create incoming Flight objects?
- A
An Airport automatically owns a separate persistent store for Flights
- B
The context is stored in the FlightAware response as an attribute
- C
A fetch request can create objects without any context
- D
A managed object knows the context from which it was obtained
Correct answer
DA managed object knows the context from which it was obtained
Explanation
Core Data objects know the context they came from. An Airport instance method can use that context for creating or fetching related objects without requiring the caller to pass it each time.
- A
- 49
When a FlightAware flight contains airport and airline codes as strings, how does the example turn them into Core Data relationships?
- A
It uses the strings directly as managed object contexts
- B
It looks up or creates the corresponding Airport and Airline objects, then assigns those objects to the Flight
- C
It changes the Flight entity into an Airport entity before saving
- D
It stores the codes only as sort descriptors and lets Core Data infer the relationships later
Correct answer
BIt looks up or creates the corresponding Airport and Airline objects, then assigns those objects to the Flight
Explanation
The external flight data contains codes, while the Core Data model uses relationships to Airport and Airline objects. The example resolves those codes to objects and assigns them to the Flight.
- A
- 50
Why does the example fetch all Airports and Airlines for filter pickers using FetchRequests?
- A
Fetching all objects prevents the app from creating relationships
- B
A FetchRequest converts each Airport into a string automatically
- C
The picker choices then reflect the database and can update when objects are added
- D
The pickers require every object to be copied into UserDefaults first
Correct answer
CThe picker choices then reflect the database and can update when objects are added
Explanation
The filter view uses FetchRequests for all Airports and Airlines, then presents those objects in pickers. The fetched collections stay current as new objects enter the database.
- A
- 51
When does the example trigger a new incoming-flight fetch after the user edits the filter?
- A
When the destination has changed and the user completes the filter action
- B
Whenever the user changes any text field, even before confirming the filter
- C
Every time a fetched Flight changes its arrival property
- D
Only when the user changes the airline but leaves the destination unchanged
Correct answer
AWhen the destination has changed and the user completes the filter action
Explanation
The example compares the draft destination with the current destination and fetches incoming flights when the destination differs after the user completes the filter action.
- A
- 52
Why must the main flight-search predicate include origin and airline conditions in addition to destination when those filters are selected?
- A
Otherwise the fetch would match only the destination and could show flights that fail the selected origin or airline filters
- B
Because Core Data ignores destination conditions whenever an airline is selected
- C
Because SwiftUI pickers do not retain selected object relationships
- D
Because sort descriptors cannot sort flights unless origin and airline are also predicates
Correct answer
AOtherwise the fetch would match only the destination and could show flights that fail the selected origin or airline filters
Explanation
A destination-only predicate does not enforce the other selected filters. The example builds a predicate that always includes destination and adds origin and airline conditions when those values are present.
- A

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