I only have one managed object context.
I have modified the managed object store though.
There are certain fields that I would like to add to every managed object. It would take far too much time to add them all one by one to every object in my system, so I decided to add them programmatically to the managed object model. In the application delegate, I first do:
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
Then I iterate through all the entities in the model and modify them and set the new property array for each one:
for (NSEntityDescription *entity in entities) {
NSAttributeDescription *idAttribute = [[NSAttributeDescription alloc] init];
[idAttribute setName:@"id"];
[idAttribute setAttributeType:NSStringAttributeType];
[idAttribute setOptional:NO];
[idAttribute setIndexed:YES];
and so on.
It seems to mostly work fine. I run into problems when I am trying to add an object to a new one-to-one relationship I created. I create the objects from the same managed object context:
self.action = [NSEntityDescription insertNewObjectForEntityForName:@"MobileObjectAction" inManagedObjectContext:managedObjectContext];
self.user = [NSEntityDescription insertNewObjectForEntityForName:@"MobileUser" inManagedObjectContext:managedObjectContext];
When I get to
[user setAction:action];
I get: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'action' between objects in different contexts ...
What am I doing wrong?