I'm working on a Core Data app (for iPhone 3.0, though I don't think that really makes a difference here) and it will need to ship with a "starter" database filled with data. With SQLite, I would just have the App copy the populated database from the bundle into the App's documents directory on first launch and then load that database - all the information would come along with it and we'd be ready to go. But with Core Data, I'm not really sure if I can just save the Persistent store to the App bundle and copy it before having Core Data start doing its thing. Will this cause any problems? There is quite a bit of initial data, so I don't want to package it in another format and have to parse through it.
+9
A:
Yes, you can copy over a pre-populated persistent store.
I created a Mac app that populates a store. It is copied into my bundle and at start, copied to the Docs directory. This works fine. I am told the Core Data Books example was developed the same way.
Please note this doesn't mean you can just copy over any old SQLite file. It has to be a Core Data persistent store, though I think you understand that based on your question.
Hunter
2009-05-29 22:16:28
Do you think it's ok to programatically populate the persistent store on my App's first run? Wouldn't this make my app considerably smaller to transfer since I would have to a while SQLite database included?
leolobato
2009-09-01 13:17:30
If your app can do this quickly, without pissing off a waiting user, then that seems like a fine way to do it.
Hunter
2009-09-01 22:33:28
Be careful. Unless your data can be generated algorithmically, it seems that the file-size savings you get by populating your data at runtime would be offset by the amount of data-populating code you need to include in your application.
Chris Gillum
2009-12-21 05:25:34
A:
Actually there is a trick: you must name the file you are going to copy over with an extension other than ".sqlite", ".bin" will do. Otherwise Xcode will change the contents of the file when it copies it into the app during the build phase and it won't load.
zaph
2009-12-14 15:31:16