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: Sebastian Celis <email@hidden>
- Date: Mon, 15 Jun 2009 07:16:40 -0500
On Mon, Jun 15, 2009 at 2:56 AM, Quincey
Morris<email@hidden> wrote:
> What implementation of the 'tag' and 'book' properties are you using? (That
> is, how are setTag and setBook defined?)
I am just using @property and @dynamic to define all of these.
SCBook.h:
@property (nonatomic, retain) NSSet *tags; // This is the
Book->BookTag relationship.
SCBook.m:
@dynamic tags;
SCBookTag.h:
@property (nonatomic, retain) SCBook *book;
@property (nonatomic, retain) SCTag *tag;
SCBookTag.m:
@dynamic book;
@dynamic tag;
SCTag.h:
@property (nonatomic, retain) NSSet *bookTags;
SCTag.m:
@dynamic bookTags;
> (It's a bit confusing, btw, that you've called the Book->BookTag
> relationship 'tags' instead of 'bookTags'.)
Sorry. I'd change how I talk about them now but I fear that would just
lead to more confusion.
> 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.
Yeah, I initially had the same thought and I have checked and double
checked the data model. The inverses all look good, and all the
validation conditions are very lax. The only one I have set is to
ensure that a Tag must have at least one BookTag under the bookTags
relationship.
> 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?
I am not using garbage collection. Do I actually need to retain
anything in the code example in my original message? From creating the
SCBook to calling [[book tags] count] I am not retaining anything,
but I was under the impression that I did not have to since this would
all happen before the autorelease pool empties itself. I went ahead
and tried retaining all of them in my example and [[book tags] count]
is still 1.
> Whatever gives you an actual error message is your best attack point for
> solving your problem.
Yeah, I spent hours tying to determine why I was getting a "dangling
reference to an invalid object" error when deleting an existing
BookTag and adding a new BookTag in the same commit. I kept trying to
simplify the scenario and then started to notice that even the initial
creates were not acting as I expected. I eventually decided that
adding two BookTags was still causing me to scratch my head, and
because that seemed like a really simple scenario, I thought I would
start there. Do you have any suggestions for how I would approach the
dangling reference error? It points to the BookTag object I deleted
(both Book and Tag in that object point to nil). I definitely call
[managedObjectContext deleteObject:bookTag] but I guess that somehow,
somewhere in the object tree someone still has a reference to it.
It was this that caused me to think that my model was incorrect in how
the object tree is constructed.
> 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?
The debugger is telling me that all of the relationships look fine
except for the Book->BookTags relationship. The set of BookTags only
contains the first BookTag object I created (scifi, in my example).
The 'dystopian' tag, on the other hand, is not there. However, the
relationships all look fine in the tag2 and bookTag2 objects. bookTag2
points at both the book and tag2, and tag2 points at bookTag2. It's
all very strange.
> 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.
Yeah, I did try calling that before [[book tags] count], but sadly it
made no difference.
Thank you very much for the time you have spent helping me thus far.
This issue is really killing me.
- Sebastian
_______________________________________________
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