Problem adding an entity to a to-many relationship

  • I am trying to get my head round Core Data and Bindings. I have
    created a simple core data model consisting of two entities called
    Course and Student. There is a to-many relationship from Course to
    Student. I have created a view with one table for Courses and another
    for students. There is a button for adding courses. That works fine. I
    also have a button which when clicked gets a CSV file containing
    student details. For each student in the file I want to create a new
    entity and have the following code in my NSPersistentDocument subclass:

    NSManagedObjectContext *context = [self managedObjectContext];
    Student *student = [NSEntityDescription
    insertNewObjectForEntityForName:@"Student"
    inManagedObjectContext:context];

    This seems to work OK. I then update the fields with data from the CSV
    file.

    The problem comes when I try to add the student to its owning course.

    I get the course from the array controller using:

    Course *course = [[coursesController selectedObjects] objectAtIndex:0];

    This seems to return the correct entity.

    Then I then add the student using the predefined accessor:

    [course addStudentsObject:student];

    This fails in the call [NSBinder
    _invokeSelector:withAguments:onKeyPath:ofObject:mode:raisesForNotApplicableKeys
    :]

    I'm using the latest version of XCode and have got garbage collection
    switched on.

    I hope someone can sort me out.

    Cheers
    Keith
  • On Dec 30, 2008, at 07:00, Keith Lander wrote:

    > Then I then add the student using the predefined accessor:
    >
    > [course addStudentsObject:student];
    >
    > This fails in the call [NSBinder
    > _invokeSelector:withAguments:onKeyPath:ofObject:mode:raisesForNotApplicableKeys
    > :]

    The first step is to find out what the failure is. Most likely it's an
    exception, in which case your run log window should contain an error
    message. What's the error message?
  • Can you share the code for this call:

    [course addStudentsObject:student];

    That method is not part of the API so I am guessing you wrote it.  If
    we can get a peak into there we can see what the issue is.

    Marcus S. Zarra
    Zarra Studios LLC
    Simply Elegant Software for OS X
    www.zarrastudios.com

    On Dec 30, 2008, at 8:00 AM, Keith Lander wrote:

    > I am trying to get my head round Core Data and Bindings. I have
    > created a simple core data model consisting of two entities called
    > Course and Student. There is a to-many relationship from Course to
    > Student. I have created a view with one table for Courses and
    > another for students. There is a button for adding courses. That
    > works fine. I also have a button which when clicked gets a CSV file
    > containing student details. For each student in the file I want to
    > create a new entity and have the following code in my
    > NSPersistentDocument subclass:
    >
    > NSManagedObjectContext *context = [self managedObjectContext];
    > Student *student = [NSEntityDescription
    > insertNewObjectForEntityForName:@"Student"
    > inManagedObjectContext:context];
    >
    > This seems to work OK. I then update the fields with data from the
    > CSV file.
    >
    > The problem comes when I try to add the student to its owning course.
    >
    > I get the course from the array controller using:
    >
    > Course *course = [[coursesController selectedObjects] objectAtIndex:
    > 0];
    >
    > This seems to return the correct entity.
    >
    > Then I then add the student using the predefined accessor:
    >
    > [course addStudentsObject:student];
    >
    > This fails in the call [NSBinder
    > _invokeSelector:withAguments:onKeyPath:ofObject:mode:raisesForNotApplicableKeys
    > :]
    >
    > I'm using the latest version of XCode and have got garbage
    > collection switched on.
    >
    > I hope someone can sort me out.
    >
    > Cheers
    > Keith
  • Keith,

    > I am trying to get my head round Core Data and Bindings.

    This can be a lot easier to do for the technologies separately.  You
    can create a Foundation tool project, add the Core Data framework, and
    work out your Core Data learning in a simple command line tool.  The
    "Low Level Core Data Tutorial" is, ironically, an good demystifying
    introduction.

    <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreDataUtilityTu
    torial/Articles/chapter_1_section_1.html
    >

    > The problem comes when I try to add the student to its owning course.
    >
    > I get the course from the array controller using:
    >
    > Course *course = [[coursesController selectedObjects] objectAtIndex:
    > 0];
    >
    > This seems to return the correct entity.
    >
    > Then I then add the student using the predefined accessor:
    >
    > [course addStudentsObject:student];
    >
    > This fails in the call [NSBinder
    > _invokeSelector:withAguments:onKeyPath:ofObject:mode:raisesForNotApplicableKeys
    > :]

    You might try the Cocoa Bindings Reference Guide's chapter on Working
    with a Controller's Selection.  Also, a full backtrace from gdb at the
    point of failure would be useful.  You can get one using a breakpoint
    on objc_exception_throw

    - Ben
  • On 6 Jan 2009, at 06:15, Ben Trumbull wrote:

    >>
    >> I am trying to get my head round Core Data and Bindings.
    >
    > This can be a lot easier to do for the technologies separately.  You
    > can create a Foundation tool project, add the Core Data framework,
    > and work out your Core Data learning in a simple command line tool.
    > The "Low Level Core Data Tutorial" is, ironically, an good
    > demystifying introduction.

    Thanks for the advice. I think you are right. I did read somewhere
    that it is best not to tackle core data and bindings at the same time.
    I have struggled so far, so now is the time to take a step back. I was
    about to give up altogether and return to Java, but ...

    Cheers
    Keith