Xcode relationship bindings

Discussion in 'Mac Programming' started by Fnordis Cord, Dec 9, 2009.

  1. macrumors newbie

    Fnordis Cord

    Joined:
    Dec 9, 2009
    Location:
    Belgium
    #1
    Hello. I feel a bit intimidated to ask an obvious question like this.

    How do I display a list of EntityB for a selected EntityA?

    In a Core Data, document based application, I define EntityA and EntityB in the datamodel. EntityA has a 1-to-many relationship to EntityB, call it contents (which has a reverse to-one relationship, say container).

    I successfully display, add, remove objects in seperate tables. For this, I create two NSArrayController (one for each entity), specify the corresponding entity in the inspector window (Array Controller Attributes), and bind the instances to the File's Owner's managed object context (inspector window, Array Controller Bindings pane, Parameters).
    Then, I also bind the Value of individual Table Columns Bindings to the aforementioned NSArrayControllers, arrangedObjects.

    This basically is what the wizard does when I drag Core Data Entity objects from the Library to my window (unless I'm missing something).

    I tried 2 approaches to display a list of (related) EntityB objects of a selected EntityA.

    First, I just bound the Table View content to "EntityA Array Controller", Controller Key "selection", Model Key Path "contents".
    After this, I bound the individual Table Column Value to "EntityA Array Controller", Controller Key "selection", Model Key Path "contents.propertyOfEntityB".

    Second, I created a seperate Array Controller that binds to "EntityA Array Controller" selected objects, call it "Selected EntitiesB". Then bound the table column to the arranged objects of this Array Controller.

    Nothing worked. I tried different combinations of selection and selectedObjects. I tried telling the NSTableView which objects it took.

    At the time of this writing I am using Xcode 3.2.1, in OSX 10.6.2.

    I suspect this can easily be done without writing any code at all. Or am I overestimating the power of Xcode's Core Data?
     
  2. macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #2
    For your array controllers:

    Controller for EntityA
    Bind: managedObjectContext
    To: File's Owner
    Model Key Path: managedObjectContext

    Controller for EntityB
    Bind: contentSet (as to-many relationships are unordered)
    To: Controller for EntityA
    Controller Key: selection
    Model Key Path: contents

    Watch the "deletes objects on remove" option. If that is unchecked, I believe the EntityB controller leaves the objects in the store and only removes the link from the relationship. I suspect you would want the object deleted unless you plan to use a separate array controller to populate a popup button for selecting existing objects in new objects.

    Table view columns should then be bound to the arrangedObjects from the associated controllers.

    Though it does not apply directly to Core Data (and uses contentArray rather than contentSet for the detail controller binding), I would recommend reading "Creating a master-detail interface" from "Cocoa Bindings Programming Topics". It covers the setup from the generated interface in more detail.

    Kind regards

    Luke
     
  3. thread starter macrumors newbie

    Fnordis Cord

    Joined:
    Dec 9, 2009
    Location:
    Belgium
    #3
    many thanks

    Luke, you have no idea how much you just lowered global frustration levels.
    After reading your brief explanation, I got my app to work in 5 minutes. Considering I've been learning obj-c 2 (I recommend Programming in Objective-C 2.0 by Stephen G Kochan), flushing out opaque and tedious Apple docs, read Xcode 3 Unleashed by Fritz Anderson (which I seriously dissuade anyone from reading), going through each and every tutorial I could find (even on youtube), browsing forums and discussions FOR MONTHS now.. you cannot imagine how happy you just made me with this crystal clear bit of genius. You are worth your weight in gold and I hope you are obese.
    Thank you.
     
  4. macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #4
    I'm actually quite happy with my weight now, but thanks for the sentiment. :) Core Data was what I started with when I began programming, so I can understand the frustration.

    There is a new book on Core Data available through Pragmatic Programmers. I do recommend the reading. Though there are several areas where the book could be improved (a few technical errors and some other areas that aren't as clear as they could be), it would probably help with learning the framework. The book is obviously much more guided than the Apple documentation, so it should be an easier learning tool.

    http://www.pragprog.com/titles/mzcd/core-data

    Best of luck.

    Luke
     

Share This Page