|Dec 9, 2009, 09:38 AM||#1|
Xcode relationship bindings
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?
Last edited by Fnordis Cord; Dec 9, 2009 at 10:05 AM.
|Dec 9, 2009, 11:12 AM||#2|
For your array controllers:
Controller for EntityA
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.
|Dec 10, 2009, 04:09 AM||#3|
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.
|Dec 10, 2009, 08:35 AM||#4|
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.
Best of luck.
|Thread Tools||Search this Thread|
|thread||Thread Starter||Forum||Replies||Last Post|
|Bindings question: default value||Qaanol||Mac Programming||4||Nov 20, 2013 12:15 PM|
|NSUserDefaults, Bindings, NSPopup Button Question||mdeh||Mac Programming||0||Aug 27, 2013 11:47 PM|
|Can't use action-combo key-bindings in Xcode 4?||printz||Mac Programming||0||Mar 19, 2013 11:35 AM|
|Cocoa bindings questions||jeanlain||Mac Programming||3||Oct 6, 2012 03:20 PM|
|NSPopupButton Bindings||knightlie||Mac Programming||0||Jun 4, 2012 11:32 AM|
All times are GMT -5. The time now is 08:16 AM.