4

I am attempting to complete my first iOS app and could use some help in efficiently merging two persistent stores. By "efficiently", I mean dealing with adds and changes without having to manually do a record-by-record analysis to end up with a single store with no duplicates. I foresee continual updates to the database we supply the user and will need to update their database without destroying any new records they may have added to database we originally supplied them.

Currently, at start-up the app checks for a persistent store in the user's Documents directory - call it "Main.sqlite". If it doesn't find the file, then it copies SeedData.sqlite from the app bundle to Main.sqlite in the user's Documents directory using NSFileManager method, -copyItemAtPath:toPath:error:.

Next, if it DOES find Main.sqlite in the Documents directory, then I would like to merge SeedData.sqlite into Main.sqlite. Both stores have exactly the same structures. The merge criteria would be: SeedData record MATCHES Main record - take no action. SeedData record NOT FOUND in Main - add SeedData record to Main.

The number of records in each store would be on the order of a few thousand.

I know Sunny dealt with a more complicated scenario two years ago. What is an efficient way to Merge two iOS Core Data Persistent Stores?

I'm wondering if anything has changed in the last 2 years that would make the solution even easier and what, if any, impact the change in iOS 7 to the default journal mode from rollback journaling to Write Ahead Logging has on the solution.

I've read Marcus Zarra's blog and Ray Wenderlich's guys say that migration is for moving a database to a new structure and not for moving new data between two identical structures - although it seems as though Sunny disagrees.

What's the best approach?

Community
  • 1
  • 1
AnrosApps
  • 131
  • 8
  • http://www.objc.io/issue-4/core-data-migration.html hope this link will help – karan Apr 16 '14 at 06:56
  • Thanks, karan, but again, that is all about moving the same data from one model to an entirely new model. I'm looking to merge two different sets of data that both come from the same model. – AnrosApps Apr 16 '14 at 15:32
  • @Borca Did you find any solution? I am also looking for a solution http://stackoverflow.com/questions/33694048/coredatamigrate-data-from-bundled-db – anoop4real Jan 29 '16 at 08:24
  • @anoop4real The approach I ended up using can be found here: [How to Save Data When Using One ManagedObjectContext and PersistentStoreCoordinator with Two Stores](http://stackoverflow.com/questions/23665714/how-to-save-data-when-using-one-managedobjectcontext-and-persistentstorecoordina/25435240#25435240) – AnrosApps Feb 03 '16 at 22:28
  • So you used two stores? Instead is there a way to merge things from bundled db to the db in the app? – anoop4real Feb 04 '16 at 06:00

0 Answers0