Core Data
Core Data stack, NSManagedObject, fetch requests, relationships, migrations, performance
1What is NSPersistentContainer in Core Data?
What is NSPersistentContainer in Core Data?
回答
NSPersistentContainer encapsulates the entire Core Data stack including NSPersistentStoreCoordinator, NSManagedObjectContext, and NSPersistentStore. It simplifies initialization by automatically managing configurations and reduces boilerplate code compared to old manual approach. Using NSPersistentContainer is the recommended practice since iOS 10 for quick and thread-safe Core Data setup.
2What is the main role of NSManagedObjectContext?
What is the main role of NSManagedObjectContext?
回答
NSManagedObjectContext acts as an in-memory scratch pad between the application and persistent storage layer. It manages CRUD operations, tracks all changes with undo/redo, and coordinates saves to persistent store. A context is bound to a specific queue and must never be shared between threads. Use perform() for any thread-safe interaction.
3What is an NSManagedObject?
What is an NSManagedObject?
回答
NSManagedObject is the base class for all persistent objects in Core Data. It represents a unique instance of an entity defined in the data model with a unique objectID. NSManagedObject automatically manages properties via KVC, bidirectional relationships, and object state (inserted, updated, deleted) within the context. Subclassing NSManagedObject allows adding business logic.
How to initialize an NSFetchRequest to retrieve entities?
Which object is responsible for communication with the storage layer?
+19 面接問題