Core Data for local persistence

7397
0

Core Data is Apple's object graph and persistence framework, essential for complex data models and offline support. I define entities and relationships in the .xcdatamodeld file, then generate NSManagedObject subclasses. The NSPersistentContainer encapsulates the stack—model, store coordinator, and contexts. I perform all operations on the main context for UI updates and background contexts for imports. Fetch requests with predicates and sort descriptors query data efficiently. Core Data provides automatic change tracking, undo/redo, and iCloud sync. For simple persistence, I use UserDefaults or Codable with FileManager, but Core Data excels with relationships and large datasets.