问题描述
我有一个预先存在的NSManagedObjectModel,我用XCode GUI创建。我想创建一个排序的抓取属性,XCode 3.2的GUI不支持。我在创建我的NSPersistentStoreCoordinator之前做所有这些,因为我知道你不能修改一个NSManagedObjectModel后,对象图管理器开始使用它。我创建了NSFetchedPropertyDescription这样:
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]];
当我调用
[fetchedPropertyDescription setFetchRequest:fetchRequest];
我得到以下异常:
NSInvalidArgumentException: Can't use fetch request with fetched property description (entity model mismatch).
推荐答案
我需要在设置之前将NSFetchedPropertyDescription添加到NSEntityDescription NSFetchedPropertyDescription上的NSFetchRequest。
I needed to add the NSFetchedPropertyDescription to the NSEntityDescription before setting the NSFetchRequest on the 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];
这篇关于如何以编程方式创建NSFetchedPropertyDescription?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!