开发者

What is the common way to insert master data to sqlite in Core Data?

I'm studying using Core Data. After I create the entity in Xcode, I want to insert some data, which is master data, what is the common way or should I say best-practice to achieve this?

I googled a little and foun开发者_Python百科d out where the real sqlite file that generated by xcode located. It looks like /Users/<Username>/Library/Application Support/iPhone Simulator/User/Application/<Application GUID>/Documents/<database name.sqlite> Now I'm wondering if I edit the sqlite (just to insert data) file directly is a proper way?


If what you mean is a preset database setup for all new database instances, there's a couple of ways to do this.

  1. Programmatically , you can insert Entities into the managedObjectContext with a bunch of code. This will get be slightly painful for anything other than a fairly trivial dataset but the advantage is that you can use all of NSPersistantDocument for virtually free.

  2. Use your app to create the dataset and then save it in the apps bundle as a read-only copy which you can clone as needed when a user creates a new database. A bit messier if you are using the NSPersistantDocument architecture.

  3. Do what you said in the first place . Use an sqlite3 client to inject data into the database to create a read-only copy like 2, but the risk is that there is more to the database structure that CoreData inserts which you dont know about so you might put a bit of work in to find that CoreData cannot read the db after you mess with it in sqlite3. I haven't tried but it might work.

IMHO 1 is the best practice as you get a bunch of free behavior from NSPersistantDocument


The only sane way to populate a data set into a Core Data sqlite store is programatically.

For iPhone apps, it's common for people to write small OS X apps to generate sqlite files, which can then be added to the app bundle.

The Core Data sqlite store is complex, and Apple officially considers it an opaque data store. Attempting to modify or create data in the store manually is likely to cause data corruption. Don't do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜