The Data Model and Persistent Store

The persistent store, which is sometimes referred to as a backing store, is where Core Data stores its data. By default on the iPhone, Core Data will use a SQLite database contained in your application’s documents folder as its persistent store. But this can be changed without impacting any of the other code you write by tweaking a single line of code. We’ll show you the actual line of code to change in a few moments.

Every persistent store is associated with a single data model, which defines the types of data that the persistent store can store. If you expand the Resources folder in the Groups & Files pane in Xcode, you’ll see a file called CoreData.xcdatamodel. That file is the default data model for your project. The project template we chose gave us a single persistent store and an associated data model. Single-click CoreData.xcdatamodel now to bring up Xcode’s data model editor. As you design your own applications, this is where you’ll build your application’s data model.

Take a look at the data model editor. Notice the single rounded rectangle in the middle of the editing window. That rectangle is known as an entity. In effect, an entity is like a class definition, wrapping your various data elements under a single umbrella. This particular entity has the name Event, and it features sections for Attributes and Relationships. There’s a single attribute, named timeStamp, and no relationships.

Click off the entity rectangle. The title bar should turn a light pink. Click back on the entity, and it will turn blue, indicating the entity is selected.

The entity was created as part of this template. If you use this template to create your own Core Data application, you get the Event entity for free. As you design your own data models, you’ll most likely delete the Event entity and create your own entities from scratch.

A moment ago, you ran your Core Data sample application in the simulator. When you pressed the plus icon, a new instance of an Event was created. Entities, which we’ll look at more closely in a few pages, replace the Objective-C data model class you would otherwise use to hold your data.

Do not change the type of persistent store once you have posted your application to the App Store. If you must change it for any reason, you will need to write code to migrate data from the old persistent store to the new one, or else your users will lose all of their data— something that will likely make them quite unhappy.

Source of Information : Apress More iPhone 3 Development Tackling iPhone SDK 3


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner