如何以编程方式创建 NSFetchedPropertyDescription?
我有一个使用 Xcode GUI 创建的预先存在的 NSManagedObjectModel
。我想创建一个已排序的获取属性,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).
I have a pre-existing NSManagedObjectModel
that I created with the Xcode GUI. I want to create a sorted fetched property, which Xcode 3.2's GUI doesn't support. I do all of this before creating my NSPersistentStoreCoordinator
because I know you can't modify a NSManagedObjectModel
after an object graph manager has started using it. I created the NSFetchedPropertyDescription
thusly:
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]];
When I call
[fetchedPropertyDescription setFetchRequest:fetchRequest];
I get the following exception:
NSInvalidArgumentException: Can't use fetch request with fetched property description (entity model mismatch).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦使用托管对象模型创建对象图(即存在使用它的上下文或存储之后),您就无法更改它。该模型定义了图中所有对象的属性和关系。如果你即时改变它,图表就会变成乱码。
这也适用于获取的属性。来自 NSFetchProperyDescription 文档:
You can't alter a managed object model once it has been used to create an object graph i.e. after there is context or a store that uses it. The model defines the properties and relationships of all the objects in the graph. If you change it on the fly the graph turns into gibberish.
This applies to fetched properties as well. From the NSFetchProperyDescription docs:
在设置
NSFetchedPropertyDescription
上的NSFetchRequest
之前,我需要将NSFetchedPropertyDescription
添加到NSEntityDescription
中。正确的步骤如下:
I needed to add the
NSFetchedPropertyDescription
to theNSEntityDescription
before setting theNSFetchRequest
on theNSFetchedPropertyDescription
.The proper steps are below: