newbie problems with master-detail relationship

  • I have created a test program  with a master detail relationship. It
    contains 2 Entities:
    "Master" and "Detail". I have generated custom classes  that sub-
    class Managed Object
      Class automatically for these entities, with accessors with Xcode.
      I have then dragged the entities into interface builder,creating a
    tableview bound to their
    respective object arrays.  I have bound  "Detail Array
    Controller"Content set to "Master Array Controller".
    I have set the Object class name for each Array controller to their
    respective classes.
    This works  fine if I want to add new rows using the interface.
    Next  I created an method: add in  MyDocument,  because I want to be
    able to add new "Master" and "Detail"
    records, programatically at runtime. When the method: add (below) is
    invoked, the the new row shows up for the Master
    table but not the Detail row. If invoke the method a second time, I
    get the following error message:
    [<NSCFString 0x2d04c> valueForUndefinedKey:]: this class is not key
    value coding-compliant for the key child.
    I have the following accessors for the key child in the Detail.h :

    // Declaration

    - (NSString *)child;
    - (void)setChild:(NSString *)value;

    Am I adding instantiating the  detail object correctly?
    Is somthing I should be doing in bindings that I am missing?
    Can you see anything wrong with method: add?

    Thanks in advance for your help?

    - (IBAction)add:(id)sender {


    NSManagedObjectContext *managedObjectContext = [self
    managedObjectContext];
        [[managedObjectContext undoManager] disableUndoRegistration];

    master =[NSEntityDescription
      insertNewObjectForEntityForName:@"Master"
      inManagedObjectContext:managedObjectContext];

    [master setParent:@"test"];

    detail =  [NSEntityDescription
    insertNewObjectForEntityForName:@"Detail"
    inManagedObjectContext:managedObjectContext];


    [detail setChild:@"test"];

        [master  addDetailObject:  @"detail"];

    [managedObjectContext processPendingChanges];
    [[managedObjectContext undoManager] enableUndoRegistration];

    }
  • On Oct 29, 2006, at 1:58 PM, shaun bear wrote:

    > Am I adding instantiating the  detail object correctly?
    > Is somthing I should be doing in bindings that I am missing?
    > Can you see anything wrong with method: add?

    It looks like you might not be clear how Core Data relationships
    work, but it's hard to tell since you didn't describe your model.

    It would be unusual (but not impossible) for a "parent" or a "child"
    to be a raw NSString object. It would usually be another managed
    object of some sort. There's no way for the NSString to "own" another
    object like that.

    At minimum, this line:

    [master  addDetailObject: @"detail"];

    Should be changed to this...

    [master  addDetailObject: detail];

    ... so that a link is actually created between the "master" and
    "detail" objects. The version you have tries to create a relationship
    between the "master" object, and an NSString object with the value
    @"detail".

    Hope that's clear. This can be pretty confusing stuff when you're
    just getting started.

        - Scott
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today