3

I have set up and app which has pre-populated data that copies the database to the project's store. Using the 'CoreDataBooks example' method: Any way to pre populate core data?

For application upgrades, I want to add more data to the database but I don't want to change the existing database since new user data is stored there?

What's the best way to do this?

I'm thinking I would create a new versioned managed object model (I'm not sure if you can add a new MOM version if nothing in the schema actually changes), for new versions, read the currently running MOM version, migrate the MOM to the latest version and manually add the new records in the code that have not been added since the currently running MOM version.

It seems a bit tedious to manually add new records in the code. Does that sound right or is there a more elegant way to add this new data?

Thanks!

Community
  • 1
  • 1
jyap
  • 3,516
  • 2
  • 16
  • 16

1 Answers1

0

If you change the managed object model itself e.g. add a new entity or change an existing attribute, then you need to use migration to update the existing persistent store. See the Core Data docs for details on migration.

If you just want to add new data, then you don't have any choice but to do so "manually."

Remember, Core Data is an object graph management system, not a database. An object "graph" is a "web" of interrelated objects so the only way to add data to the graph is to create new objects and set their relationships. It's not inefficient, it just the way it works.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • How would you recommend adding new data to new versions of an application? – jyap Jun 04 '11 at 23:23
  • It depends. Do you want to just replace the existing data and/or persistent store? Are you going to update the model itself? Do you want to merge the data with the users existing data? – TechZen Jun 05 '11 at 19:49
  • I'm going to mark this as answered. At first I was thinking of merging the new data but then I thought this would get really complicated to track and require more code. Instead I'm going to backup the user created data, delete the existing persistent store and copy a new pre-populated data store to the app's persistent store. – jyap Jun 06 '11 at 08:30