22
Core Data

When deciding between approaches to saving and loading for iOS applications, the first question is Local or remote? If you want to save data to a remote server, you will likely use a web service. If you want to store data locally, you have to ask another question: Archiving or Core Data?

Your Homepwner application used keyed archiving to save item data to the filesystem. The biggest drawback to archiving is its all-or-nothing nature: To access anything in the archive, you must unarchive the entire file, and to save any changes, you must rewrite the entire file. Core Data, on the other hand, can fetch a subset of the stored objects. And if you change any of those objects, you can update just that part of the file. This incremental fetching, updating, deleting, and inserting can radically improve the performance of your application when you have a lot of model objects being shuttled between the filesystem and RAM.

Object Graphs

Core Data is a framework that lets you express what your model objects are and how they are related to one another. It then takes control of the lifetimes of these objects, making sure the relationships are kept up to date. When you save and load the objects, Core Data makes sure everything is consistent. This collection of model objects is often called an object graph, as the objects can be thought of as nodes and the relationships as vertices in a mathematical graph.

Often you will have Core Data save your object graph to a SQLite database. Developers who are used to other SQL technologies might expect to treat Core Data like an object-relational mapping system, but this mindset will lead to confusion. Unlike an ORM, Core Data takes complete control of the storage, which just happens to be a relational database. You do not have to describe things like the database schema and foreign keys – Core Data does that. You just tell Core Data what needs storing and let it work out how to store it.

Core Data gives you the ability to fetch and store data in a relational database without having to know the details of the underlying storage mechanism. This chapter will give you an understanding of Core Data as you add persistence to the Photorama application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset