3
votes

I need to add some attributes to my core data model and I am having a terrible time getting lightweight migration to work!

I keep getting the error "Cant merge models with two different entities named blah".

Here's what I've done ...

  1. Added this code to my app delegate.

    • (NSPersistentStoreCoordinator*)persistentStoreCoordinator {

      //blah blah

      NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
      [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
      [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

      //blah blah

      return _persistentStoreCoordinator;
      }

  2. Clicked on my data model, went to Design > Data Model > Add Model Version.

  3. Made my changes to the one with the LOWEST number, basically adding a few attributes.

  4. Deleted all the managed files produced from my previous model, sent them to trash, then created new ones from the new model.

  5. Cleaned all targets.

  6. Build and go.

ERROR.

Please please help. I've tried the above in numerous different ways, and loads of other stuff, each time going back to a clean copy of my project and starting again, and nothing has got me past this error.

Thanks!

2

2 Answers

4
votes

Well, once again, another 6 hours of my life completely wasted because Apple are a bunch of ... well, I'll stop there.

Anyway, thanks to this lovely person: http://linkroller.com/fullpage/ad/13754/?http://iphonedevelopment.blogspot.com/2009/09/core-data-migration-problems.html I was able to solve the problem.

You follow the steps I already followed, then you need to find the following method:

- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }

    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
    return managedObjectModel;
}

and change it to:

- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Foo" ofType:@"momd"];
    NSURL *momURL = [NSURL fileURLWithPath:path];
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

    return managedObjectModel;
}

where foo is the name of you xcdatamodeld file.

AAAAAARGH.

1
votes

I had fixed the core data migration

pls follwing this steps

  1. Go AppDelegate.m write function

    -(NSPersistentStoreCoordinator *)persistentStoreCoordinator
     {
       if (__persistentStoreCoordinator != nil)
       {
         return __persistentStoreCoordinator;
         }
    
            NSString *databaseFilePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"sampleiOS.sqlite"];
    
            NSURL *storeUrl = [NSURL fileURLWithPath: databaseFilePath];
    
            NSDictionary *_option = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES],            NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
            NSError *error = nil;
    
            __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    
           if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:_option error:&error]) {
    
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    
         }
    
       return __persistentStoreCoordinator;
     }
    
  2. then select example.xcdatamodeld file

  3. click the Editor menu on the top => add model version => create the new model version "example 2.xcdatamodel" then click finish button.

  4. now show two core data model version one is source "example.xcdatamodel" another one is destination "example 2.xcdatamodel".

  5. now add an attribute or entity in your new version datamodel "example 2.xcdatamodel". then click the group data model "example.xcdatamodeld". After that set current version to be newly created data model "example 2.xcdatamodel".

How to set current Version

select show utilities => show inspector => versioned core data model . then set current version.