adesso blog

People working together

All the variables and constants that you allocate in Swift are stored in memory, so they get lost when users quit the app.

Persistence is saving data to a place where it can be re-accessed and retrieved upon restart of the device or app. So, that is longer than a life cycle the running app and keeps it available to the user.

UserDefaults is by far the easiest way to persist your data to your device. It’s like a dictionary that automatically saves its contents. With all that;

  • It saves data into p.list
  • We need to keep the size below 1 MB. Because its reads and writes all in one go.

But if the data is getting bigger and complex?

Every app has a data layer. In a cooking app data might include recipes, ingredients. Within this data there are often relationships. For example, a recipe may call for many ingredients. While an ingredient should be free to appear in multiple recipes. The recipe app’s data includes structure that is clearly defined object types, and relationships between instances of the types.

We use CoreData to manage data layer.

Persistence is just one aspect of data layer management.

CoreData (data layer management framework) saves data into something called a persistent store. The store is where the data lives. There are 3 different types of stores CoreData supports on iOS;

  • SQLite Store (default)
  • Binary Store
  • In-memory Store

SQLite is almost always the right choice of your persistent store in a SQL relational database.

Binary store can be appropriate when you always need the database to be read and written in its entirety.

In-memory store can be appropriate when you have a small data model can fit in memory all at once and that does not need to be saved to disk. (like cache)

CoreData abstracts the persistent store’s details. That means you won’t usually interact with the store directly. That provides a common interface for saving and fetching data no matter what kind of store sits below. Whatever type of store you choose you’ll always use the same (CoreData). And, you won’t need to learn a database-specific language to manage your data.

Are There Other Ways to Save Data?

Yes. Although Core Data is a great solution for persisting data that has structure and relationships, other solutions are better suited for other types of data. For settings or small pieces of data, UserDefaults is most appropriate. If you want to store data directly on the file system, you can use the Foundation framework to serialize and write data to files. If you are familiar with SQLite, then you could use it directly instead of through Core Data. There are also ways of caching transient, downloaded data such as the result of a network request.

Third party databases and persistence frameworks like Realm and Firebase provide useful features like syncing local and remote data, and cross-platform support for iOS and Android. But, while powerful, these frameworks often lack UIKit integration, and they may not implement the full extent of Core Data features. It’s up to you to consider the tradeoffs.

Save this page. Remove this page.