views:

281

answers:

2

I want to ship static read-only data for use in my Core Data model. The problem is that there are obviously different persistent store types and I don't know if the format of those types is supposed to be opaque or if I'm supposed to be able to construct them by hand.

Right now I just have a plist and it's very small (maybe 30 entries total).

Should I just write code to import the plist into my data store when the app is first installed, or is there some way I can ship a hand-constructed initial version of the data store file?

(I'm using the default sqlite persistent store.)

+2  A: 

I would not try to hand-construct it, but you certainly should execute an import and save a final Core Data SQLite file to ship with your app.

I plan to write a small mac utility (using the same data model) to generate the Core Data SQLite file for my iPhone app (the import is actually from a web server). Then, I will add the file that was persisted by the utility into my iPhone app's project.

gerry3
Thanks. After I posted that I noticed that the "Recipes" application (one of Apple's iPhone sample code apps that's intended to demonstrate Core Data usage) comes with a Recipes.sqlite default database. If I can depend on persistent stores creates on OS X to be portable to the iPhone or whatever else Core Data eventually supports then that sounds like a good way to go, but I haven't been able to find any documentation assuring me that such a thing is safe/supported or what Apple would want you to do....
Nimrod
The SQLite store format on the Mac is identical to the one on the iPhone. Apple engineers have encouraged us to go back and forth between the two, so I assume this will continue on for the foreseeable future. I create a read-only Core Data database in a Mac application, and ship it with an iPhone application with no troubles. You can use the exact sample data model for both. For a true read-only store (one you keep in the application bundle), you might also want to set the NSReadOnlyPersistentStoreOption on your persistent store.
Brad Larson
+1  A: 

To add a bit to the answer to my own question, I noticed that the Recipes sample code application comes with a default sqlite backing store:

// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
    NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Recipes" ofType:@"sqlite"];
    if (defaultStorePath) {
        [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
    }
}

But then again, for another purpose it comes with some static read-only data in a plist file! (TemperatureData.plist) So go figure....

Nimrod