Persistence & networking

Core Data / SwiftData, URLSession, Codable, and background tasks. Know when Core Data earns its complexity over plain SQLite.

About 23 min read3-question knowledge check

The mental model

The iOS data layer answers the same question as its Android counterpart — *where does the screen's data come from, and how does it stay fresh and available offline?* — with the same answer: **a local store is the single source of truth, and the network is a background writer that keeps it fresh.** The screen reads from Core Data / SwiftData; a URLSession fetch decodes JSON with Codable and writes into that store; the store's change notifications push the update to the UI. Reading is always fast and always available; the network is an enhancement, never a blocker.

The iOS-specific decision — and the one interviewers zero in on — is **which persistence tool, and whether its complexity is earned.** The options form a ladder: UserDefaults for a handful of settings, a plain file or SQLite for simple structured data, Core Data / SwiftData for a rich object graph with relationships, querying, change tracking, and background contexts. Core Data is powerful but genuinely complex — contexts, the persistent container, threading rules — so the senior instinct is not to reach for it by default. The question "when does Core Data earn its complexity over plain SQLite?" is the archetypal iOS data probe, and the honest answer is: when you need relationships, faceted querying, undo, or reactive UI over a large graph — not for a flat list of cached items, where SQLite (or even a file) is simpler and faster to reason about.

**SwiftData (iOS 17+)** is Apple's modern, Swift-native layer over the same Core Data engine: you annotate a class with @Model and query with @Query in SwiftUI, getting reactive persistence with far less ceremony. It is a common choice for new SwiftUI apps. A ModelContext is not thread-safe, so background SwiftData work uses a @ModelActor with its own context and passes PersistentIdentifiers, not models, across actors. Core Data remains where you need its maturity, fine-grained control, or must support older OS versions.

The unifying idea: **the store is the source of truth, Codable + URLSession is the pipe that fills it, and background execution is what lets it sync without blocking the UI or draining the battery.** Pick the store to match the data's shape, not the hype, and the offline experience falls out for free.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision