如何以编程方式创建NSFetchedPropertyDescription?

时间:2011-04-12 16:13:02

标签: objective-c xcode core-data fetched-property

我使用Xcode GUI创建了预先存在的NSManagedObjectModel。我想创建一个已排序的fetched属性,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).

2 个答案:

答案 0 :(得分:1)

一旦将托管对象模型用于创建对象图,即在有上下文或使用它的商店之后,您就无法对其进行更改。该模型定义图中所有对象的属性和关系。如果你动态改变它,图表会变成乱码。

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

  

获取属性说明   可编辑,直到它们被一个人使用   对象图管理器。这可以让你   动态创建或修改它们。   但是,一旦使用了描述   (当托管对象模型为   它所属的是与a相关联的   持久性商店协调员),它必须   不(实际上不能)改变。这个   在运行时强制执行:任何尝试   改变模型或其任何主题   在模型与a关联之后   持久性存储协调器导致   被抛出的异常。如果你需要   修改正在使用的模型,创建   副本,修改副本,然后   丢弃旧的物体   模型。

答案 1 :(得分:1)

NSFetchedPropertyDescription上设置NSEntityDescription之前,我需要将NSFetchRequest添加到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];
//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];