What's the correct way to ship static (read-only) data in Core Data persistent store?

大憨熊 提交于 2020-01-12 10:10:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!