加载先前的型号版本 [英] Load a previous model version

查看:146
本文介绍了加载先前的型号版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用initWithContentsOfURL:构造函数加载NSManagedObjectModel模型:

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];

但是这只能让我访问模型的最新/最新版本。是否可以使用相同的 momd 文件加载以前的版本?如何?

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?

推荐答案

实际上,您可以加载以下版本的旧版本:

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;
 }

只需将AppModel替换为您的型号名称即可。

Just replace AppModel with your model name.

我使用它来摆脱涉及iCloud的粘性手动迁移情况。搜索高和低,无法找到这里。

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.

这篇关于加载先前的型号版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆