Prefilled version of Core Data? Prefilled version of Core Data? json json

Prefilled version of Core Data?


There's a reasonable tutorial about pre-loading over at Ray Wenderlich's site.

Generally - create a separate project, parse the JSON file into a core data database. Create your real project, copy the Object model and the database file to this new project.

Now, at app start up, check if the database exists in the document's directory, and if it does not, copy your prefilled one from your app bundle.

Make sure that the Persistent Store Coordinator works with the database in the documents folder and not the one in the app bundle.

Update June 2012

I've got a small example project on GitHub called PromNight which demonstrates using an Xcode Workspace with an iPad project and a OS X project to preload the data for Core Data. This uses an object model that is shared between the the two applications which helps to keep changes in sync when preloading.


Core Data uses a backing store, which is essentially a sqlite database (or, on Mac OS, optionally an XML file). You should simply add that file to your app's bundle and ship it with the app. As far as getting the data into the database, here's what I would do:

  1. Write some code to import the data from whatever format you have it in.
  2. Run that code.
  3. Copy the sqlite file off of the device or from the simulator.
  4. Add the newly created sqlite file to your project in Xcode.

I wouldn't hand-create the sqlite file, since Core Data does some "voodoo" behind the scenes and messing with the sqlite can break things. Also, I've seen developers use multiple targets. for the import. This way, they can write the code in a compiler conditional and then not have to worry as much about project maintenance. For example:

#ifdef kImportTarget//run core data import#else// run the Core Data stack setup from an existing file#endif


The Core Data database is just a SQLite database file. You can deliver it in your main bundle and then copy it to your documents folder before associating it with your persistent store coordinator.