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).
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:
I needed to add the
NSFetchedPropertyDescription
to theNSEntityDescription
before setting theNSFetchRequest
on theNSFetchedPropertyDescription
.The proper steps are below: