问题
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.)
回答1:
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.
回答2:
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....
回答3:
Not many people know this, but you can actually use core data on OSX and use that store file then on the iOS. So essentially write some code that uses exactly the same schema and your model objects (they should all compile and work on OSX).
OSX development isn't really that hard to get your handle on, if you know iOS SDK :-)
HTH
来源:https://stackoverflow.com/questions/2018127/whats-the-correct-way-to-ship-static-read-only-data-in-core-data-persistent-s