Re: [off-list] Re: CoreData: Editing a model with XCode corrupts reflefive relationships
Re: [off-list] Re: CoreData: Editing a model with XCode corrupts reflefive relationships
- Subject: Re: [off-list] Re: CoreData: Editing a model with XCode corrupts reflefive relationships
- From: Charilaos Skiadas <email@hidden>
- Date: Mon, 18 Jul 2005 17:45:35 -0500
On Jul 18, 2005, at 4:12 PM, Frank Illenberger wrote:
Am 18.07.2005 um 23:09 schrieb Jim Correia:
On Jul 18, 2005, at 4:51 PM, Frank Illenberger wrote:
has anybody already observed the following phenomenon?
I have an entity that uses a reflexive relationship for building
a tree. Until now this relationship worked fine. But if I now add
another isolated entity to my model, this relationship stops
working. This means, that an object add to one side of the
relationship is not automatically put into the other side. It
seems as if the model file has been damaged because even if I
revert to the old model state by pressing undo in the XCode model
editor, the wrong behaviour stays. Only if I revert to a backup
copy of my model file, the said relationship starts working again.
What does your model look like. Do you use any entity inheritance
(called parent in the UI)?
I ran into a problem with a previous layout of my model, and filed
a bug. I was able to work around it by modifying the model at
runtime before passing it to the persistent store coordinator.
Jim
Yes, I am using entity inheritance. The base entity contains the
reflexive relationship that gets damaged.
Does it only get damaged with regards to the subentity? If so, it is
a well known problem, and it is the same one that Jim is talking
about. Basically, the inverse relationship to a relationship that a
subentity inherits from its parent is broken, and needs to be fixed
at run-time when the model is set. My workaround, per an Apple
engineer's suggestion, was to implement managedObjectContent in
MyDocument to fix re-establish the connection as follows:
- (id)managedObjectModel
{
// This code is necessary to bypass a bug, upon which the inverse
relationships
// of relationships that subentities inherit from their parents
are not set properly.
NSManagedObjectModel *model = [super managedObjectModel];
NSArray *entities = [model entities];
NSEnumerator *en = [entities objectEnumerator];
NSEntityDescription *item = nil;
while (item = [en nextObject]) {
NSEntityDescription *parent = [item superentity];
if (parent != nil) {
NSDictionary *relationships = [item relationshipsByName];
NSDictionary *parentRelationships = [parent
relationshipsByName];
NSEnumerator *en2 = [relationships keyEnumerator];
id key = nil;
NSRelationshipDescription *relationship = nil;
while (key = [en2 nextObject]) {
relationship = [relationships objectForKey:key];
NSRelationshipDescription *parentInverse =
[[parentRelationships objectForKey:key] inverseRelationship];
if ([relationship inverseRelationship] == nil &&
parentInverse != nil) {
[relationship
setInverseRelationship:parentInverse];
}
}
}
}
return model;
}
It is a generic solution, I haven't tested it much, hopefully it will
be of some use to you. I believe Jim also has a test case with his
work-around, that just fixes the particular entities involved. I
tried to have the method run through the entire model and fix
whatever could be broken. Use at your own risk of course.
What exactly did you change in the model at runtime to make it work?
Frank
Haris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden