Explore key concepts and practical scenarios involving data persistence in Swift using Core Data and UserDefaults. Assess your understanding of storage options, typical use-cases, and fundamental operations for managing persistent data in Swift-based applications.
Which type of data is most suitable for storage in UserDefaults, such as saving a user's preferred theme setting?
Explanation: UserDefaults is designed for small amounts of simple key-value data like settings or user preferences, making it a fit for theme selection. Large image files and multimedia streams are too heavy and are generally stored elsewhere. Complex relational data is more appropriate for a database like Core Data.
Which feature allows Core Data to efficiently fetch only the data you need, such as loading a specific user's profile from a large dataset?
Explanation: Fetch Requests are used in Core Data to retrieve specific data from the persistent store, making querying efficient. Save Actions relate to saving changes, not fetching. Memory Pools and Thread Locking are not features used for selective data retrieval in this context.
If your Swift app needs to store and manage relationships between multiple entities, such as users, tasks, and projects, which persistence solution should you use?
Explanation: Core Data is suited for managing complex data models with relationships, such as users linked to tasks and projects. UserDefaults is for simple data, not complex structures. Property Lists are not designed for managing relationships, and Temporary Memory is not persistent.
What method can be called to ensure UserDefaults immediately writes data to disk after changing a value?
Explanation: The synchronize() method was historically used to immediately write changes to disk, though it is now rarely necessary. commit(), flushAll(), and saveData() are not valid UserDefaults methods for this purpose.
Which of these types can be directly stored in UserDefaults without additional conversion?
Explanation: Strings are a basic supported type for UserDefaults, allowing for direct storage. UIImage and UIColor are not supported directly and need conversion. URLSession is an object that cannot be stored in UserDefaults.
When working with Core Data, what is the main component used to manage and track changes to your data objects before saving to the store?
Explanation: The Managed Object Context is responsible for tracking changes to data objects and coordinating with the persistent store. A Data Scheduler doesn't exist in this context. App Delegate manages app lifecycle, and the Store Coordinator helps connect the context to the database but does not track objects itself.
How would you remove a specific value, such as a saved username, from UserDefaults?
Explanation: removeObject(forKey:) is the correct method to remove a stored value tied to a specific key. The other options are not valid UserDefaults method names and will not work for this purpose.
Which Core Data store type is most commonly used for persistent data storage on a device?
Explanation: SQLite is the default and most commonly used format for persistent Core Data storage. HTTPLoader and DataExporter are not Core Data store types, and Memory only is used for temporary, non-persistent storage.
If you try to retrieve a value from UserDefaults using a key that was never set, what result will you get?
Explanation: UserDefaults returns nil or a specified default value when a key isn't found. It does not throw an error, return random data, or cause a crash—these are incorrect and would indicate a serious flaw.
If you update your Core Data model schema (such as adding a new attribute to an entity), what process helps ensure existing app data remains accessible?
Explanation: Migration is used in Core Data to help your app adapt to changes in the data model while preserving existing data. Compilation does not apply to data models, erasing the database would protect nothing, and encryption does not relate to schema updates.