1. Home
  2. Explore
  3. Computer Science
  4. Core Data in Swift

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Core Data in Swift

  • 46Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Core Data Model
Core Data stores data in a SQL database but lets Swift code interact with it through an
approach.

46 questions

  1. 1
    Core Data stores data in a SQL database but lets Swift code interact with it through an
    _____
    approach.
    Hint

    Think of the programming model built around objects and their properties.

    Answer

    object-oriented

    Explanation

    Core Data uses SQL behind the scenes while presenting an object-oriented interface, so developers do not need to write SQL statements for ordinary use.

  2. 2
    The central map in Core Data connects objects and their vars to tables and
    _____
    in a relational database.
    Hint

    Relational database tables are made up of columns and individual records.

    Answer

    rows

    Explanation

    Core Data maps object-oriented data to the tables and rows of its underlying relational database.

  3. 3
    In Xcode’s Core Data model editor, the objects that will be stored are represented as
    _____
    entities.
    Hint

    These are the named object types, such as Flight, Airport, and Airline.

    Answer

    Core Data

    Explanation

    The model editor defines entities such as Flight, Airport, and Airline, along with their attributes and relationships.

  4. 4
    Xcode generates the classes that represent the entities and their stored
    _____
    from the data model.
    Hint

    These are the properties associated with each object.

    Answer

    vars

    Explanation

    After entities and attributes are defined in the model, Xcode generates code for classes that represent them and their stored vars.

  5. 5
    Developers can add methods and computed vars to generated Core Data classes using
    _____
    Hint

    This Swift feature adds functionality to an existing type without changing its original definition.

    Answer

    extensions

    Explanation

    Extensions let developers add behavior and computed vars to the generated entity classes.

  6. 6
    The database model can define object
    _____
    graphically, such as linking a Flight to its Airport.
    Hint

    These express how one stored object is connected to another.

    Answer

    relationships

    Explanation

    The model editor supports graphical relationships between entities, in addition to defining their attributes.

  7. 7
    Core Data entity attributes are optional by default because a database object may be incomplete or
    _____
    Hint

    Consider what might happen if a record is damaged or only partly created.

    Answer

    corrupted

    Explanation

    The lecture explains that database values may be missing if an object was only partly created or the database was corrupted, so generated properties are optional by default.

  8. 8
    A Core Data property marked optional in the model is not necessarily a Swift
    _____
    Hint

    The same word is used for two different notions of optionality here.

    Answer

    Optional

    Explanation

    The model’s optional setting concerns whether a value may be absent in the database; the lecturer cautions that this is not the same as Swift Optional.

  9. 9
    The Core Data stack’s persistent container holds the database, while an NSManagedObjectContext provides the application's
    _____
    onto it.
    Hint

    It is the access point through which database operations are performed.

    Answer

    window

    Explanation

    The persistent container holds the Core Data database, and the managed object context is the window through which code accesses database objects.

  10. 10
    The context is needed when creating objects, fetching objects, and
    _____
    changes to the database.
    Hint

    This operation commits changes so they persist.

    Answer

    saving

    Explanation

    Core Data operations use the context, including creating objects, fetching them, and saving changes.

  11. 11
    When a new project is created with Core Data enabled, Xcode provides a blank data
    _____
    to define entities and attributes.
    Hint

    It is the file edited in Xcode’s graphical entity editor.

    Answer

    model

    Explanation

    Enabling Core Data creates a blank model that developers can use to define their database entities, attributes, and relationships.

  12. 12
    The Core Data setup places code that creates the persistent store in the
    _____
    Hint

    This setup location is distinct from the code that injects the context into views.

    Answer

    AppDelegate

    Explanation

    The generated Core Data stack in AppDelegate creates the persistent container and database store.

  13. 13
    The SceneDelegate obtains the managed object context from the persistent container and places it into SwiftUI’s
    _____
    Hint

    SwiftUI uses this mechanism to make values available to descendant views.

    Answer

    Environment

    Explanation

    The SceneDelegate retrieves the context and passes it through the Environment so SwiftUI views can access the database.

  14. 14
    A sheet or popover creates a new view environment, so the managed object context should be
    _____
    into it.
    Hint

    The new presentation needs access to the same database context as its parent.

    Answer

    passed

    Explanation

    Unlike views nested in a parent’s body, a sheet or popover has its own environment. The context must be explicitly passed into that environment.

  15. 15
    Saving a context can fail, so the call to context.save() must be handled with Swift’s
    _____
    mechanism.
    Hint

    This keyword is used when calling a throwing function.

    Answer

    try

    Explanation

    The save operation can throw, for example if storage is unavailable or another error occurs, so it must be called with try or another error-handling form.

  16. 16
    Core Data’s optional CloudKit integration can mirror database contents to
    _____
    so they are available across a user’s devices.
    Hint

    It is Apple’s cloud service for synchronizing data across devices.

    Answer

    iCloud

    Explanation

    When CloudKit support is enabled, Core Data data can be mirrored to iCloud and made available on the user’s devices.

  17. 17
    An NSFetchRequest specifies the entity to fetch and can use a predicate to select records and sort descriptors to control their
    _____
    Hint

    These descriptors determine how returned results are arranged.

    Answer

    order

    Explanation

    A fetch request identifies the entity and can specify both filtering criteria and result ordering through sort descriptors.

  18. 18
    An NSPredicate describes the
    _____
    that fetched objects must satisfy.
    Hint

    It determines which records should be included in the results.

    Answer

    criteria

    Explanation

    A predicate defines which database objects match a request, such as flights arriving before now from a particular origin.

  19. 19
    In a predicate format, the placeholder %@ is paired with the corresponding
    _____
    values.
    Hint

    These are supplied after the format string to fill in the requested values.

    Answer

    arguments

    Explanation

    Predicate formats use placeholders such as %@, followed by arguments that provide the values to compare against.

  20. 20
    The predicate TRUEPREDICATE matches every object, making it useful when fetching
    _____
    airports.
    Hint

    The predicate evaluates to true for every record.

    Answer

    all

    Explanation

    TRUEPREDICATE is always true, so it can be used to fetch all objects of an entity, such as all airports.

  21. 21
    An NSSortDescriptor specifies a property to sort by and whether the ordering is ascending or
    _____
    Hint

    This is the reverse of ascending order.

    Answer

    descending

    Explanation

    A sort descriptor identifies the property used for sorting and whether results should be ordered ascending or descending.

  22. 22
    A FetchRequest’s sort descriptors let the database perform sorting efficiently on the database
    _____
    Hint

    This contrasts with sorting the results only after they reach the application.

    Answer

    side

    Explanation

    The lecture notes that SQL databases are good at sorting, so sort descriptors allow ordering to happen on the database side.

  23. 23
    The context’s fetch method may throw; using try? makes a failed fetch produce
    _____
    Hint

    This result differs from a successful fetch with no matching records.

    Answer

    nil

    Explanation

    With try?, a fetch error becomes nil. A successful fetch with no matches instead returns an empty array.

  24. 24
    A successful fetch with no matching records returns an empty
    _____
    rather than nil.
    Hint

    A successful fetch returns a collection of matching objects, even when there are none.

    Answer

    Array

    Explanation

    An empty Array indicates the fetch succeeded but found no matches; nil from try? indicates that the fetch failed.

  25. 25
    A Core Data entity’s generated objects are ObservableObjects, so an individual Flight can be presented in SwiftUI using
    _____
    Hint

    This property wrapper observes a single reference-type model object in a view.

    Answer

    @ObservedObject

    Explanation

    Core Data objects such as Flight, Airport, and Airline are ObservableObjects and can be observed by SwiftUI views.

  26. 26
    The lecture notes that an observed Core Data object may require an explicit objectWillChange.send() call after its values are
    _____
    Hint

    The notification tells observing views that the object is about to update.

    Answer

    changed

    Explanation

    The lecturer found that observed Core Data objects did not always signal changes automatically, so objectWillChange.send() could be called explicitly.

  27. 27
    SwiftUI’s @FetchRequest wraps results in a
    _____
    Results collection rather than an ordinary Array.
    Hint

    The type name begins with the past-tense form of “fetch.”

    Answer

    Fetched

    Explanation

    The property wrapper provides a FetchedResults collection containing the requested Core Data objects.

  28. 28
    FetchedResults is a Collection, so it can be supplied to SwiftUI’s
    _____
    view for presenting repeated items.
    Hint

    This SwiftUI container creates content for each element in a collection.

    Answer

    ForEach

    Explanation

    FetchedResults conforms to Collection, making it suitable as the collection iterated over by ForEach.

  29. 29
    Unlike a one-time fetch, @FetchRequest acts like a standing
    _____
    that tracks database changes.
    Hint

    It continually checks which objects meet the stated criteria.

    Answer

    query

    Explanation

    A FetchRequest property wrapper continually reflects objects that match its request, rather than returning only a one-time snapshot.

  30. 30
    When database changes alter which objects match a FetchRequest, SwiftUI updates the results and redraws the
    _____
    Hint

    This is the visible interface presented to the user.

    Answer

    UI

    Explanation

    FetchRequest keeps the view’s results synchronized with matching database changes, causing the UI to update.

  31. 31
    A FetchedResults collection can be converted to an Array by using the Array
    _____
    Hint

    This is the construction operation that accepts a Collection.

    Answer

    initializer

    Explanation

    Because FetchedResults is a Collection, it can be passed to the Array initializer to produce an Array.

  32. 32
    A Flight’s destination and origin properties each refer to one Airport, so each is a
    _____
    relationship.
    Hint

    The relationship points from one flight to a single airport.

    Answer

    To One

    Explanation

    Each Flight has one destination Airport and one origin Airport, making those Flight-side relationships To One.

  33. 33
    An Airport can have multiple arriving Flights, so its flightsTo relationship is configured as
    _____
    Hint

    The airport is connected to a collection of flights rather than a single flight.

    Answer

    To Many

    Explanation

    Many Flight objects can share the same destination Airport, so the Airport’s flightsTo relationship is To Many.

  34. 34
    When a Flight’s origin is set to an Airport, Core Data automatically updates the Airport’s corresponding
    _____
    relationship.
    Hint

    It is the Airport-side relationship for flights departing from that airport.

    Answer

    flightsFrom

    Explanation

    Core Data maintains both sides of a configured relationship, so assigning a Flight’s origin also updates the Airport’s flightsFrom collection.

  35. 35
    When a Flight’s destination is set, Core Data automatically adds it to the destination Airport’s
    _____
    relationship.
    Hint

    It is the Airport-side relationship for flights arriving there.

    Answer

    flightsTo

    Explanation

    The inverse relationship is maintained automatically: setting a Flight’s destination updates the Airport’s flightsTo collection.

  36. 36
    The generated Airport relationship may be an Objective-C NSSet of Any, so an extension can expose a Swift
    _____
    of Flight instead.
    Hint

    This native Swift collection represents unique elements.

    Answer

    Set

    Explanation

    The lecture uses computed properties to convert the Objective-C NSSet relationship into a typed Swift Set of Flight.

  37. 37
    A computed relationship property can return an empty Set when the underlying database relationship is
    _____
    Hint

    This is the absence value the computed property is designed to guard against.

    Answer

    nil

    Explanation

    The computed property hides optionality by returning an empty Set when the stored relationship value is nil.

  38. 38
    Flight, Airport, and Airline inherit from
    _____
    Hint

    This is the Core Data superclass used when an array may contain different entity types.

    Answer

    NSManagedObject

    Explanation

    Using NSManagedObject as a common superclass allows values from different Core Data entity classes to be stored in one array.

  39. 39
    In the flight-search predicate, destination is always included, while origin and airline criteria are added only when those values are
    _____
    Hint

    Optional filters are included only when the user has selected them.

    Answer

    present

    Explanation

    The predicate is constructed programmatically: it always filters by destination and conditionally adds origin and airline conditions.

  40. 40
    The demo first retrieves FlightAware data, stores it in Core Data, and then builds the UI by reading from the
    _____
    Hint

    After loading, the interface no longer depends directly on the network response.

    Answer

    database

    Explanation

    The application loads FlightAware data into Core Data and uses the database as the source of data for the UI.

  41. 41
    Core Data objects know the managed object context they came from, allowing an instance method to access that context without always
    _____
    it as an argument.
    Hint

    This avoids repeatedly supplying the same context to instance methods.

    Answer

    passing

    Explanation

    An object retrieved from Core Data can access its managedObjectContext, so code can use that context for related database operations.

  42. 42
    When a FlightAware response arrives, the app can look up a Flight by its ident, then update or
    _____
    the matching database object.
    Hint

    If no record is found, a new object is needed.

    Answer

    create

    Explanation

    The update workflow searches by ident; it updates the existing Flight if found or creates one when no matching Flight exists.

  43. 43
    Airport and Airline lookup helpers first search the database by a unique code and create a record if none is
    _____
    Hint

    The helper reuses existing records rather than duplicating them.

    Answer

    found

    Explanation

    The withICAO and with-code helpers fetch an existing object by its code, creating one only when the search returns no match.

  44. 44
    When a new Airport is created before its FlightAware details arrive, its identifier can be set immediately while other information is filled in
    _____
    Hint

    The network response arrives later, after the initial object has been returned.

    Answer

    asynchronously

    Explanation

    The Airport can initially contain its ICAO, while FlightAware information such as coordinates arrives later and updates the database asynchronously.

  45. 45
    For a Core Data query, predicates and sort descriptors must refer to the database
    _____
    names, even when Swift extensions expose cleaner computed properties.
    Hint

    These are the stored model properties used by the database query.

    Answer

    field

    Explanation

    Fetch predicates and sort descriptors operate on fields in the data model, so renamed or computed Swift properties cannot replace the stored model names in the query.

  46. 46
    If a context save fails, a do-catch block can catch the error and inspect its
    _____
    description.
    Hint

    This error description is intended to be presented or interpreted in a user’s locale.

    Answer

    localized

    Explanation

    The demo shows catching a save error and printing its localizedDescription to help identify the failure.

1 of 46
Question 1 of 46

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent