1

NSManagedObjectModel我有一个使用 Xcode GUI 创建的预先存在的文件。我想创建一个 Xcode 3.2 的 GUI 不支持的排序获取属性。我在创建我之前完成所有这些,因为我知道在对象图管理器开始使用它之后NSPersistentStoreCoordinator你不能修改它。NSManagedObjectModel我因此创建了NSFetchedPropertyDescription

NSManagedObjectModel *managedObjectModel = ... // fetch from my mainBundle

NSEntityDescription *fetchedPropertyEntityDescription = [entitiesByName objectForKey:@"MyEntity"];

NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:fetchedPropertyEntityDescription];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"myPredicateProperty == $FETCH_SOURCE"]];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"mySortProperty" ascending:YES]]];

NSFetchedPropertyDescription *fetchedPropertyDescription = [[[NSFetchedPropertyDescription alloc] init] autorelease];
[fetchedPropertyDescription setFetchRequest:fetchRequest];
[fetchedPropertyDescription setName:@"myFetchedProperty"];

NSEntityDescription *entityDescription = [entitiesByName objectForKey:@"MyFetchSourceEntity"];

[entityDescription setProperties:[[entityDescription properties] arrayByAddingObject:fetchedPropertyDescription]];

当我打电话

[fetchedPropertyDescription setFetchRequest:fetchRequest];

我得到以下异常:

NSInvalidArgumentException: Can't use fetch request with fetched property description (entity model mismatch).
4

2 回答 2

1

一旦托管对象模型被用于创建对象图,即在存在使用它的上下文或存储之后,您就不能更改托管对象模型。模型定义了图中所有对象的属性和关系。如果您即时更改它,则图表会变成乱码。

这也适用于获取的属性。来自 NSFetchProperyDescription 文档:

获取的属性描述在被对象图管理器使用之前是可编辑的。这允许您动态创建或修改它们。但是,一旦使用了描述(当它所属的托管对象模型与持久存储协调器相关联时),它就不能(实际上不能)更改。这是在运行时强制执行的:在模型与持久存储协调器关联之后,任何对模型或其任何主体进行变异的尝试都会导致抛出异常。如果您需要修改正在使用的模型,请创建一个副本,修改该副本,然后丢弃旧模型中的对象。

于 2011-04-12T20:11:21.883 回答
1

我需要在设置之前添加NSFetchedPropertyDescription到.NSEntityDescriptionNSFetchRequestNSFetchedPropertyDescription

正确的步骤如下:

NSManagedObjectModel *managedObjectModel = ... // fetch from my mainBundle

NSEntityDescription *fetchedPropertyEntityDescription = [entitiesByName objectForKey:@"MyEntity"];

NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:fetchedPropertyEntityDescription];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"myPredicateProperty == $FETCH_SOURCE"]];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"mySortProperty" ascending:YES]]];

NSFetchedPropertyDescription *fetchedPropertyDescription = [[[NSFetchedPropertyDescription alloc] init] autorelease];
//DON'T DO THIS HERE, AN ERROR WILL OCCUR
//[fetchedPropertyDescription setFetchRequest:fetchRequest];
//
[fetchedPropertyDescription setName:@"myFetchedProperty"];

NSEntityDescription *entityDescription = [entitiesByName objectForKey:@"MyFetchSourceEntity"];

[entityDescription setProperties:[[entityDescription properties] arrayByAddingObject:fetchedPropertyDescription]];

//DO THIS HERE INSTEAD
[fetchedPropertyDescription setFetchRequest:fetchRequest];
于 2011-04-14T02:47:52.750 回答