Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

CoreData: Inverse relationships not maintained (to-one on one size, to-many on the other, with abstract classes)



I have a data model with abstract entities in the hierarchy. The Item object knows about its root container (to one relationship), and the container object knows about its items (too many relationship.)

If I manually set the value for one side of the relationship, the other side isn't maintained automatically. I've built a fictitious app which demonstrates the problem.

Setting either side of the relationship doesn't automatically cause the inverse to update.

NSManagedObjectContext *context = [self managedObjectContext];
id library = [NSEntityDescription insertNewObjectForEntityForName: @"LibraryNode" inManagedObjectContext: context];
id libraryItem = [NSEntityDescription insertNewObjectForEntityForName: @"MP3Item" inManagedObjectContext: context];
id libraryItem2 = [NSEntityDescription insertNewObjectForEntityForName: @"MP3Item" inManagedObjectContext: context];


    [libraryItem setValue: @"Fred" forKey: @"name"];
    [libraryItem2 setValue: @"Bob" forKey: @"name"];

    // Try setting up this side of the relationship

    [libraryItem setValue: library forKey: @"root"];

NSLog(@"%@", libraryItem);
NSLog(@"%@", library);
NSLog(@"library contains item = %d", [[library valueForKey: @"items"] containsObject: libraryItem]);


    // now try setting up the other side with the second object

[[library mutableSetValueForKey: @"items"] addObject: libraryItem2];

NSLog(@"%@", libraryItem2);
NSLog(@"%@", library);
NSLog(@"libraryItem2 root = %@", [libraryItem2 valueForKey: @"root"]);



Full example is on my iDisk:

http://homepage.mac.com/jimcorreia/tmp/BrokenInverseRelationships.zip

What am I doing wrong?

Thanks,
Jim


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.