iOS Development iOS Data Persistence 2 — Questions and Answers
Question 1: What does `NSFetchRequest` do in Core Data?
- Defines a query to retrieve managed objects from the persistent store (Correct answer)
- Creates new managed objects in the context
- Migrates data between model versions
- Observes changes in the managed object context
Correct answer: Defines a query to retrieve managed objects from the persistent store
`NSFetchRequest` specifies the entity, predicates, sort descriptors, and result type used to query the persistent store.
Question 2: What is a Core Data migration used for?
- Updating the persistent store schema when the data model changes (Correct answer)
- Transferring data between iCloud accounts
- Backing up Core Data to a remote server
- Merging conflicts between two managed object contexts
Correct answer: Updating the persistent store schema when the data model changes
Core Data migration updates the persistent store so it matches a new version of the managed object model without losing existing data.
Question 3: Which format is commonly used to encode Swift objects for local file storage?
- JSON via Codable
- XML via XMLParser
- Plist via PropertyListSerialization
- Both JSON and Plist are common (Correct answer)
Correct answer: Both JSON and Plist are common
Both JSON (via Codable/JSONEncoder) and Property Lists (via PropertyListEncoder) are commonly used to encode Swift objects for local storage.
Question 4: What is CloudKit used for in iOS development?
- Syncing data across a user's devices and sharing it with other users via iCloud (Correct answer)
- Downloading files from a CDN
- Storing app bundles for over-the-air updates
- Caching images from the web
Correct answer: Syncing data across a user's devices and sharing it with other users via iCloud
CloudKit provides a framework for storing structured data in iCloud and syncing it across a user's devices or sharing with others.
Question 5: Which iOS storage option is best for caching non-critical data that can be recreated?
- NSCache or the Caches directory (Correct answer)
- Keychain
- UserDefaults
- Core Data with WAL mode
Correct answer: NSCache or the Caches directory
NSCache and the Caches directory are designed for temporary data that can be regenerated, and the OS may purge them under memory or disk pressure.
Question 6: What is the main advantage of SQLite over Core Data for iOS persistence?
- Direct SQL queries and lower overhead for simple tabular data without an object graph (Correct answer)
- Better iCloud sync support
- Automatic schema migration
- Built-in encryption
Correct answer: Direct SQL queries and lower overhead for simple tabular data without an object graph
SQLite offers direct SQL access and lower overhead when you need simple, tabular storage without the complexity of an object graph.
What does `NSFetchRequest` do in Core Data?