In this article, we’ll learn about persistence in iOS. We’ll learn about what persistence is, and take a look at the three most common tools that we can use for persistence in our iOS apps.
What is persistence?
Just about every app saves information on your device. Whether it’s taking a picture with the Camera app, writing something down in Notes, bookmarking a page in Safari, or finishing part of a game, apps constantly need to save data so they can remember it even after they’re interrupted or restarted. Persistence is the ability to do just that: to save information so that it persists between restarts. Without persistence, you’d start from scratch every time you open an app—your settings, documents, photos, and more would be lost.
In iOS, there are a few different ways to persist data. Of these, the most important are UserDefaults, the file system, and Core Data. Let’s take a look at these in a little more detail.
UserDefaults is a straightforward way for storing information on iOS. With UserDefaults, we can access the defaults system, which is designed for storing user preferences and other simple data. We call this the defaults system because it often determines the app’s default state when first opened or how the app behaves by default.
We can store information with UserDefaults using key-value pairs. The value is whatever information is stored, while the key identifies what the information is for. Most common data types can be stored as values, but the key is always a
String. For example, if we want to remember that a user prefers dark mode, we might store a value of
true for the key
UserDefaults persists information by saving it in a file on the user’s device. Whenever they open an app on iOS, the system will load the entire defaults file into memory, and keep it there until the app is closed. This gives us a fast, easy way to work with small bits of information, such as settings. However, large files and complex datasets might be harder to work with in UserDefaults, or could take too long to load. For those, we’ll want to use a different persistence method.
Even if you use iOS every day, you might not know that every iOS device has a file system. This is because the file system on iOS is a little different from those of other platforms. Unlike desktop operating systems like Windows and macOS, iOS does not expose the file system to end users. Instead, apps work with the file system behind the scenes, using it internally to persist information.
Documents, images, and other large files are especially well suited to this persistence method. For example, music apps can use the file system to save songs for listening offline, and word processing apps can use the file system to persist documents.
UserDefaults and the file system give us simple ways to persist information on iOS. However, depending on what we’re creating, we might outgrow the two—especially if we’re dealing with lots of information, or if we need more advanced features. For these applications, iOS offers Core Data, a powerful framework that lets us model and work with large, complex datasets.
While Core Data offers persistent storage as a feature, it’s fundamentally about managing our different data types. To use Core Data, we first need to define our object graph, which consists of our data types and how they relate to one another. For example, if we’re creating a blogging app, our object graph could include
Comment types, with every
Post having one
Author and many
Comments. Here, Core Data’s ability to understand our data types and work with them efficiently makes it a far better choice than UserDefaults or the file system.
Core Data’s robustness comes at a cost: it can be difficult to learn and work with and can add unnecessary complexity and performance overhead to your app. If Core Data’s strengths align with your application’s requirements, it might be your best bet—especially if you’re working with complex, interrelated data types. However, if your needs are simpler, you’ll likely be better off with a simpler persistence solution.
Just about every app requires persistence. While there are many ways to persist data in iOS, the most common three are UserDefaults, the file system, and Core Data. Each of them is good for different tasks, so it’s important to weigh the benefits and drawbacks for your specific application and pick the right tool for the job.