Introduction to Data Persistence
Introduction to Data Persistence
Data persistence is an important aspect of iOS development. Not all applications fetch their data from a network, some are stored locally on device, and it is important that this data is always up to date with what the users expect.
As iOS developers, we have multiple solutions when it comes to storing data. The most basic solution revolves around using UserDefaults
, which allows us to store small amounts of data, typically primitive types, such as String
, Bool
, etc.
If we plan to store sensitive information, such as access tokens, we can use Apple’s Keychain
for that. But what if we wanted to store larger, more complex data structures? What if our application needs to store a list of movies, with complete details, cast and related metadata?
Sure, we could serialize our data into a JSON and store it locally, but when working with multiple types of data and relationships, this is usually not enough. This is where Core Data comes in.
Core Data is an ORM (Object Relational Mapper), developed by Apple, which allows us to define our entities, specify how they are related to each other, and persist them in an efficient manner.
In the following chapters, we’re going to explore how we can add Core Data capabilities in a simple Task Management application.