Should You Use Core Data Or User Defaults

Should You Use Core Data Or User Defaults

Many developers that are new to Swift and Cocoa development wonder what they should use, Core Data or the defaults system. I understand the motivation for this question, but it's the wrong question to ask. Core Data and the defaults system are very different persistence solutions. Both solutions can be used to persist data, but that is the only thing they have in common.

When to Use User Defaults?

The defaults system is ideal for storing small chunks of data, such as settings or the user's preferences. The user's defaults database is stored on disk as a property list or plist. A property list or plist is an XML file. The UserDefaults class keeps the contents of the property list in memory at runtime to improve performance.

The defaults system is nothing more than a key-value store. This makes it easy to access data, but it also means that the key-value pairs don't have an explicit relation to one another.

Drawbacks of User Defaults

The defaults system is great and I recommend using it but only when it is appropriate. As I mentioned earlier, don't use it to store large data sets or other types of data it isn't designed for, such as image data. Remember that the UserDefaults class needs to load it from disk and keeps it in memory. This is only efficient and performant if the user's defaults database is small and manageable.

When to Use Core Data?

Core Data is also a persistence solution, but it was built with performance and flexibility in mind. The data of a Core Data persistent store can be stored as an XML file or a SQLite database. You can even keep the data in-memory or create a custom persistent store to fit your project's needs.

Another key benefit of Core Data is its support for relational data sets. Xcode includes a data model editor that allows you to carefully create the data model for your project. You define entities and the relationships between these entities.

Performance is a key feature of Core Data and it is something you, the developer, don't need to worry about. Core Data only fetches the information it needs to perform the requests the application makes. This is very different from the defaults system. The UserDefaults class loads the property list into memory to improve performance and it asynchronously writes the changes back to disk at appropriate times.

Drawbacks of Core Data

The main drawback of Core Data is its learning curve. Most developers new to Core Data are intimidated by the framework and its many APIs. This is understandable and it's important to take the time to learn the fundamentals before integrating it into your project. I have been teaching Core Data for more than ten years and understand the challenges developers new to the framework face when learning Core Data.

Core Data also comes with some overhead, especially if you compare it with the lightweight defaults system. The framework excels at managing large, relational data sets, which means that it isn't efficient or recommended to use it for storing random bits of unrelated data.

Core Data or User Defaults

The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

Before I leave you, I want to mention the keychain. If you need to store sensitive information such as a password or an access token for an API, then the keychain is the best and recommended option. Don't use the defaults system for storing sensitive information.

And don't forget to research other persistence solutions. Core Data and the defaults system are not the only options you have. Take a look at SQLite or Realm. You could even store data as JSON if that is appropriate. There is no single best solution.