Load a previous model version

前端 未结 5 647
小蘑菇
小蘑菇 2021-02-12 19:31

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

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@\"MyDoc         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-12 20:03

    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.

提交回复
热议问题