views:

28

answers:

1

I have an iPhone app that has a sqlLite Core Data model that is pre-loaded with default data. I want to enable the user to restore this default data if they have modified or deleted records from the model, while retaining any new records added to the model by the user.

The sqlLite database is copied to the users documents directory on first run, so the untouched original database is available in the app package. What is the easiest way to copy records between the two databases? I assume that it involves setting up an additional persistentStoreCoordinator, or adding the original dB to the coordinator as an additional persistentStore, but the docs are skimpy on how to do this.

Thanks,

Jk

A: 

If you do not want to delete the destination store and just overwrite it then the workflow is:

  1. Stand up a second Core Data stack with the source persistent store.
  2. Fetch each entity from the source.
  3. Look for the object in the destination.
  4. If it exists, update it.
  5. If it doesn't, create it.
  6. Save the destination store.

Depending on how much data you have, this can be a very expensive operation.

Marcus S. Zarra
Thanks, Marcus. That's what I am thinking I'll have to do. The objects in question have a BOOL attribute isCustom, so I can just delete all the objects that are not custom user-created objects from the read/write persistent store, and then insert all the objects from the original dB persistent store.
Alpinista