2
votes

I just created a new version of my core data model with an extra object along with reworked relationships.

I now now have both files, Medical_Codes.xcdatamodel and Medical_Codes_ 2.xcdatamodel.

Do I have to delete the old NSManagedObject class files and recreate them?

Do I have to change my persistent store code?

- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel != nil)
    {
        return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Medical_Codes" withExtension:@"mom"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return __managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Medical_Codes.sqlite"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:[storeURL path]])
    {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Medical_Codes" ofType:@"sqlite"];

        if (!defaultStorePath)
        {
            NSLog(@"Error: Could not locate Medical_Codes.sqlite in app bundle");
            return nil;
        }

        NSError *error = nil;

        if (![fileManager copyItemAtPath:defaultStorePath toPath:[storeURL path] error:&error])
        {
            NSLog(@"Error copying sqlite from bundle to documents directory: %@, %@", error, [error userInfo]);
            return nil;
        }
    }

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return __persistentStoreCoordinator;
}
1

1 Answers

4
votes

CoreData provides various levels of migration of a database's old schema to the new schema. Sometimes you can do a lightweight migration, which means you don't have to do anything special except create your new model and generate the new managed object classes. The next time you fire up your application, the model manager does its magic and migrates your old data to the new schema.

However, when your new model is significantly different from the old model, then you are required to create a model mapping file that gives CoreData the necessary migration information to map from old to new. This mapping model file is copied into your bundle by Xcode and the model manager uses it to make the necessary migration.

You will also need to pass some additional options during the creation persistent store coordinator at runtime (So, yes, you have to change your persistent store coordinator code a tiny bit). Somewhat like:

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

if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
    ...
}

So, to answer your first question. If you added new attributes or relations, then you will need to create new managed object file(s). If all you did was modify some of the options on pre-existing attributes or relations, then your old managed object file(s) are still valid.

If you haven't yet, you should read everything Apple has written about CoreData. I have yet to read a book on the subject that covers it any better than their online documentation. In particular, read their information on versioning and migration.