views:

1603

answers:

4

I am able to create the DataModel, Entities and Properties. How do I now create the DB? Do I have to create it manually making sure that all the properties and entities are mapped?

I am following the Recipes Core Data sample and have noticed a method in RecipesAppDelegate.m:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
}

I can see a reference to the DB file here.

+2  A: 

When you create the persistent store coordinator, if you are using the SQLite persistent store type, the coordinator will automatically create the database for you if it does not already exist. You don't have to create the store file yourself.

EDIT: to clarify, the only thing you should be modifying is the Core Data object model (.xcdatamodel) file. An NSPersistentStoreCoordinator object, when it is created with a store file or the addPersistentStore: method is called on it, will do all the necessary setup of the backing store. This includes creating the file, any tables it may contain, etc.

Creating or modifying any type of persistent store yourself (especially SQLite stores) is completely unsupported by the SDK and the Core Data framework.

Tim
You mean I should create the method above and the code will create the file? What about the tables and columns in the Database?
Picflight
It's all taken care of by the object graph you designed in your data model. The persistent store coordinator will read that from your managed object model and modify its SQLite table directly. As a matter of fact, Apple's Core Data Programming Guide forbids you to interface directly with the SQLite store.
Tim
A: 

If you want an easy cheap way to look at or manage your SQLite Db you can use this firefox plugin.

It's pretty full featured and the price is right, free.

OhioDude
A: 

What about pre-populating the database? Would it be best to link the SQL source in with the build process? For a large data store this seems like an unappealing option (having to rebuild each time).

Dylan
A: 

Yes, you can¡ :) see this link... always you can¡ http://ablogontech.wordpress.com/2009/07/13/using-a-pre-populated-sqlite-database-with-core-data-on-iphone-os-3-0/

BrunoDay