SwiftData

Persistence Mid-level Swift 5.9+ iOS 17+ Reviewed By Alok Singh

Fill the blank

Fill the blank to save a new trip.

@Model final class Trip {
    var name: String
    var start: Date
    init(name: String, start: Date) { self.name = name; self.start = start }
}

struct TripList: View {
    @Query(sort: \Trip.start) private var trips: [Trip]
    @Environment(\.modelContext) private var context
    var body: some View {
        List(trips) { Text($0.name) }
            .toolbar { Button("Add") { context.____(Trip(name: "Goa", start: .now)) } }
    }
}
Options
Hint 1 of 1

How does a new object join the context?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A insert

Correct. The context tracks the new model, autosaves it, and @Query updates the list.

Why not the others?
  • B save. save takes no model; it writes pending changes.
  • C append. ModelContext isn't an array.
  • D add. There is no add method on ModelContext.

30-second answer

SwiftData (iOS 17) is a Swift-first persistence framework built on Core Data's storage. Mark classes with @Model, create a ModelContainer with .modelContainer(for:), read with @Query in views, and insert or delete through the environment's modelContext, which autosaves. Schema versions and migration plans handle model changes.

Practise saying it · 45 s. No recording, just a timer

Explain SwiftData.

Say your answer out loud, then check which of these points you covered:

  • @Model, container, context
  • @Query
  • Migrations
  • @ModelActor

Deep dive

@Model types are observable, so views update automatically.

Use #Predicate and SortDescriptor for typed queries.

@ModelActor runs background work with its own context.

Use #Index and #Unique (iOS 18) for performance and constraints.

Common mistakes

  • Passing model objects across actors.
  • Expecting every Core Data feature to exist.

Senior take & follow-ups

Seniors use SwiftData for new iOS 17+ apps and know its limits compared to Core Data.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you do background work?

    Show hint

    A @ModelActor with its own context.

  2. How do you migrate?

    Show hint

    VersionedSchema plus SchemaMigrationPlan.

Suggest a fix if something here is wrong or out of date.