How do I release an application with a pre-installed database?
I am trying to find the best way to release an application with some data preloaded. I have an application that has 2 tables. I want to populate these tables with some data. The problem is that data is not just text information. 1 entity contains about 40 attributes (numbers, strings, data to be converted), so this is not a solution for embedding in code.
Thanks for the help.
- Write a very small OS X CLI application that supports your existing Core Data stack.
- This CLI creates a pre-populated SQLite file at a known location.
- Run this CLI as part of your build routine
- Include the generated SQLite file as part of the app bundle
- At startup if target SQLite file does not exist (
NSFileManager
will report this); copy the SQLite file from the app bundle. - Start up as usual.
This makes the procedure scriptable and consistent. It reuses the existing code structure to create a pre-populated database and allows you to update it.
This is how I handle it:
I am using the default setting where the backup storage for Core data is an SQL file.
I configured my application to set up a persistent repository coordinator with a SQL file in the application's document directory.
I am creating my Core Data database filled with seeding on a simulator.
Then I go to the application documents directory on the SIM and copy the sql file to the application bundle.
At the beginning of my didFinishLaunching method in the application delegate, I check to see if the database database SQL file exists in the document directory. If not, I'll copy it from the package to the document directory.
Then I call the code that creates a persistent store coordinator that expects an sql file in the document directory. On first launch, this is the original file copied from the package. On subsequent launches, this is a working file in the document directory that contains the current data.
When the user first tries to access the data, run a check to see if there are any objects in persistent storage by performing a fetch query or getting the number of objects in persistent storage.
If the results of a fetch request are zero or the number of objects in the fetch request is 0, manually load data from some file (JSON, plist, XML) into the master data.