core-dataobject-model

Load a previous model version


I am loading a NSManagedObjectModel model with the initWithContentsOfURL: constructor like this:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyDocument" withExtension:@"momd"];
NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

However this only gives me access to the latest/current version of a model. Is it posible to load previous versions with the same momd file? how?


Solution

  • Actually you can load an older version with:

    - (NSManagedObjectModel *)managedObjectModelForVersion:(NSString *)version
    {
            NSURL *modelURL = [[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"AppModel.momd/AppModel %@",version] withExtension:@"mom"];
            NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
            return model;
     }
    

    Just replace AppModel with your model name.

    I'm using this to get myself out of a sticky manual migration situation involving iCloud. Searched high and low and couldn't find this anywhere.