简体   繁体   中英

iPhone Core Data Migration With Multiple Models

I have two models at my project, I want to allow versions to one of them. Here's what I did:

  • Selected the modelOne.xcdatamodel then Design > Data Model > Add Model Version.
  • Clicked command + i then add version to modelOne.xcdatamodel inside modelOne.xcdatamodeld
  • Modified modelOne.xcdatamodel, just added some attribute.
  • At the delegate I added the options dictionary to the NSPersistentStoreCoordinator, and I get the NSManagedObjectModel by merging the two models like:

     NSString *path1 = [[NSBundle mainBundle] pathForResource:@"modelOne" ofType:@"momd"]; NSURL *momURL1 = [NSURL fileURLWithPath:path1]; NSManagedObjectModel *modelOne = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL1]; NSString *path2 = [[NSBundle mainBundle] pathForResource:@"modelTwo" ofType:@"mom"]; NSURL *momURL2 = [NSURL fileURLWithPath:path2]; NSManagedObjectModel *modelTwo = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL2]; NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel modelByMergingModels:[NSArray arrayWithObjects:modelOne, modelTwo, nil]]; 
  • Last I did a clean and build, I got error about 'Can't find model for source store'.

Any idea how I could fix this? (Note: if I just use mergedModelFromBundles, I got another error about 'Can't merge models with two different entities named Entity1'

I had a similar problem. It turned out that the old .mom complied model file was still in the app bundle on the simulator. Apparently, when you clean a project it doesn't wipe the app bundle on the simulator. Manually deleting the app off the simulator and then cleaning before building again solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM