Restoring default records to a Core Data database
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 ski开发者_JAVA百科mpy on how to do this.
Thanks,
Jk
If you do not want to delete the destination store and just overwrite it then the workflow is:
- Stand up a second Core Data stack with the source persistent store.
- Fetch each entity from the source.
- Look for the object in the destination.
- If it exists, update it.
- If it doesn't, create it.
- Save the destination store.
Depending on how much data you have, this can be a very expensive operation.
精彩评论