Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 602
SystematicFrank Avatar asked Jun 23 '12 10:06

SystematicFrank


People also ask

How do I download an old model of BIM 360?

To download one of these previous versions, navigate to the Revit model within the ACC (or BIM 360) Document Management portal. Click on the version number under the Version column. A popup dialog will list all the previous versions of the file.

How do I restore a previous version of a BIM 360?

To recover an earlier file version in BIM 360, perform the following steps: In Document Management, browse to the desired file in the folder structure. Click on the current version (e.g. V3) in the Version column. Select the version that you want to recover.


2 Answers

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.

like image 199
Schoob Avatar answered Oct 17 '22 10:10

Schoob


If you just want to load the version of the model that's compatible with a particular existing store try:

NSError *error = nil;
NSDictionary *storeMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType 
                                                                                         URL:storeURL 
                                                                                       error:&error];
NSManagedObjectModel *oldManagedObjectModel = [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:[NSBundle mainBundle]] 
                                                                          forStoreMetadata:storeMetadata];

Note that if you use XCode version identifiers for your data model versions, the persistent store's current version identifiers are accessible through the NSStoreModelVersionIdentifiersKey entry in the store metadata dictionary.

As far as loading a particular arbitrary version is concerned, the mom files are typically located under the momd directory in your app's bundle, so you could enumerate them using NSFileManager. I believe to find one with a particular version identifier you would have to use NSManagedObjectModel's initWithContentsOfURL: initializer and then inspect the versionIdentifiers property, or use the isConfiguration:compatibleWithStoreMetadata: instance method to determine compatibility.

like image 4
Simon Lawrence Avatar answered Oct 17 '22 10:10

Simon Lawrence