Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ship my app with a pre-populated Core Data database?

Tags:

ios

core-data

My app uses Core Data and I want some default entries to be inside. What's best practices of how to do that?

like image 761
Proud Member Avatar asked May 17 '11 10:05

Proud Member


2 Answers

If you're already loading the pre-load data via a temporary routine for testing in your current code there's no reason you can't use the sqlite file it creates in the simulator's directory (no need to write a separate Mac app).

If you're not already filling that db you can still write an iOS app that does it. Odds are you've already written the methods for adding data to your store so you can use them to import the pre-load data as well.

Either way you'd grab the sqlite file from the simulator's directory and add it to your app's bundle; on first launch you'll copy it into the appropriate place in the app's directory before pointing Core Data to it. If it's really large the downside is that there will be a copy in the bundle and another on disk, but there's not much you can do about that other than grabbing the data over the network.

As others have suggested, if the amount of data is small you can just import it at first launch, using the methods you've already written for adding data as part of the normal app's workflow.

like image 123
Matthew Frederick Avatar answered Sep 29 '22 13:09

Matthew Frederick


See the CoreDataBooks example, which has sample code for copying a database at first launch.

EDIT: I've created a Core Data framework (read about it here: http://bikepress.org/?p=1120) that includes this feature.

like image 29
chris Avatar answered Sep 29 '22 13:09

chris