Re: Core Data, to-many relationships, and object graph consistency
Re: Core Data, to-many relationships, and object graph consistency
- Subject: Re: Core Data, to-many relationships, and object graph consistency
- From: Quincey Morris <email@hidden>
- Date: Mon, 15 Jun 2009 00:56:48 -0700
On Jun 14, 2009, at 12:20, Sebastian Celis wrote:
SCBookTag *bookTag1 = [NSEntityDescription
insertNewObjectForEntityForName:@"BookTag"
inManagedObjectContext:managedObjectContext];
[bookTag1 setTag:tag1];
[bookTag1 setBook:book];
What implementation of the 'tag' and 'book' properties are you using?
(That is, how are setTag and setBook defined?)
On the other hand, if I remove the [bookTag1 setBook:book] and
[bookTag2 setBook:book] lines and instead write:
NSMutableSet *theTags = [book mutableSetValueForKey:@"tags"];
[theTags addObject:bookTag1];
[theTags addObject:bookTag2];
Now, [[book tags] count] equals two! What about this situation am I
not understanding correctly? What is the correct way to do this? It
seems as though forcing me to use mutableSetValueForKey is strange,
especially because I would then have to do this for the Tag.bookTags
relationship as well instead of simply calling [bookTag1 setTag:tag1].
Either approach should work. Adding the bookTag to the book or setting
the book on the bookTag should produce the same effect. Incidentally,
it shouldn't be necessary to use 'mutableSetValueForKey:'. You should
be able to write '[book addTagsObject: bookTag1]' (using the Core-Data-
provided accessor method) so long as you follow the instructions in
the documentation for making the method known to the compiler. But the
effect is the same.
(It's a bit confusing, btw, that you've called the Book->BookTag
relationship 'tags' instead of 'bookTags'.)
In general, these particular entities and relationships are giving me
a lot of grief. For example, I have test code that can add new Tag and
BookTag objects to an existing book and save them to the persistent
store. I also have test code that can successfully delete existing Tag
and BookTag objects. However, when I run both pieces of test code at
the same time (add a new BookTag and delete an existing BookTag) and
try to save , I get an error stating that there is a dangling
reference to an invalid object (a BookTag object).
You should double check that everything is correctly defined in your
managed object model. Make sure, for example, that it shows that the
inverse relationships are recognized as inverses. Also, check any
validation conditions on your attributes. For example, if your Book-
>BookTags to-many relationship has a cardinality range of 0-1, you
probably aren't going to get the results you expect.
However, based on your description, it actually sounds more like you
have a memory management problem. Are you using garbage collection? If
not, are you sure you're retaining objects appropriately? Whatever
gives you an actual error message is your best attack point for
solving your problem.
You can use the debugger to examine your object relationships. When
you add the second object to the relationship (from either end), are
the first object's relationships getting clobbered, or are the second
objects relationships not being set up properly?
You can also try calling [moc processPendingChanges] before checking
to see if the desired inverse relationships were set up. I don't
recall ever having to use it in such circumstances, but I may be
misremembering the sequence of events.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden