从 NSPersistentDocument 中排除实体
Exclude entity from NSPersistentDocument
我有一个现有的(并且可以正常工作的)应用程序,使用 NSPersistentDocument
来保存应用程序文件。
现在我需要创建一个新实体,这个新实体与应用程序文件完全无关,它将包含应用程序缓存,所以我将用它来保存在一个单独的文件上。
我的项目包含 NSPersistentDocument
使用的 MyDocument.xcdatamodeld
,以实现新功能我创建了一个新数据模型 Cache.xcdatamodeld
并向该模型添加了一个新实体(我没有编写代码只是使用 XCode 向导)但是当我 运行 应用程序并尝试打开现有的应用程序文件时,我收到错误
The model used to open the store is incompatible with the one used to
create the store
我知道发生这种情况是因为新实体的模型配置与 NSPersistentDocument
相同,但我如何解耦它?
在数据模型中创建新配置不起作用,因为实体无法从默认配置中删除。
知道如何让 NSPersistentDocument
忽略新实体并继续使用旧数据模型吗?
我没有 post 源代码,因为这只是将新模型和实体添加到项目
来自 NSPersistDocument's managedObjectModel
property 的文档:
@property(readonly, strong) NSManagedObjectModel *managedObjectModel
Discussion
By default the Core Data framework creates a merged model from all models in the application bundle ([NSBundle mainBundle]). You can reimplement this property and return a specific model to use to create persistent stores. A typical implementation might include code similar to the following fragment:
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:@"MyModel" ofType:@"mom"];
NSURL *url = [NSURL fileURLWithPath:path];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];
我有一个现有的(并且可以正常工作的)应用程序,使用 NSPersistentDocument
来保存应用程序文件。
现在我需要创建一个新实体,这个新实体与应用程序文件完全无关,它将包含应用程序缓存,所以我将用它来保存在一个单独的文件上。
我的项目包含 NSPersistentDocument
使用的 MyDocument.xcdatamodeld
,以实现新功能我创建了一个新数据模型 Cache.xcdatamodeld
并向该模型添加了一个新实体(我没有编写代码只是使用 XCode 向导)但是当我 运行 应用程序并尝试打开现有的应用程序文件时,我收到错误
The model used to open the store is incompatible with the one used to create the store
我知道发生这种情况是因为新实体的模型配置与 NSPersistentDocument
相同,但我如何解耦它?
在数据模型中创建新配置不起作用,因为实体无法从默认配置中删除。
知道如何让 NSPersistentDocument
忽略新实体并继续使用旧数据模型吗?
我没有 post 源代码,因为这只是将新模型和实体添加到项目
来自 NSPersistDocument's managedObjectModel
property 的文档:
@property(readonly, strong) NSManagedObjectModel *managedObjectModel
Discussion
By default the Core Data framework creates a merged model from all models in the application bundle ([NSBundle mainBundle]). You can reimplement this property and return a specific model to use to create persistent stores. A typical implementation might include code similar to the following fragment:
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:@"MyModel" ofType:@"mom"];
NSURL *url = [NSURL fileURLWithPath:path];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];