Load a previous model version

前端 未结 5 634
小蘑菇
小蘑菇 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 19:44

    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.

提交回复
热议问题