Skip navigation.
 
mlCore Data and Bindings - Problem accessing attributes of related entity
FROM : Andrew Madsen
DATE : Fri Jun 30 17:52:25 2006

For background: I'm completely new to Cocoa programming and fairly 
new to programming in general. I've read through all of Stephen 
Kochan's Programming in Objective-C and am about half-way through 
Aaron Hillegass' Cocoa Programming for Mac OS X.  I've just started 
trying to develop my first Mac application.  I've run into a problem 
with Core Data and Cocoa Bindings that I've been unable to figure out.

Say I've got a data model with two entities in it. One is called 
Person, and the other is called Address. Person has a few attributes 
like name, phone number, age, etc. It has one to-one relationship to 
an Address entity. Now Address also has a few attributes like street 
number, city, state and zip code and a to-one relationship to a 
person. All fine up to this point.

Now I want to create a user interface using IB and bindings to manage 
an array of Person entities. No problem, this is easy with a table 
view, an NSArrayController and some text fields to set the various 
attributes of the Person selected in the table. Here comes the 
problem. I want to have more text fields that set the attributes of 
the Address entity related to the currently selected Person entity. I 
can't for the life of me figure out how to get this to work.

What I've got right now (and it doesn't work) is an 
NSObjectController called Address Controller set to control an 
Address entity in the Attributes Inspector. In the bindings, the 
Controller Content is bound to the Person Array Controller with a 
Controller Key of "selection" and a model key path of 
"myAddress" (the name of the relationship from Person->Address). My 
reasoning (though it may be incorrect) is that this means that the 
NSObjectConroller has one object that it controls, and it is the 
object "pointed to" by the myAddress relationship of the currently 
selected Person where Person Array Controller is bound to the table 
used to do the selection. I've got the managedObjectContext binding 
for the Address Controller bound to File's Owner (MyDocument) with a 
blank Controller Key and a Model Key Path of managedObjectContext. 
This is the same thing that the Person Array Controller's 
managedObjectContext binding is set to. I'm not sure if this is right.

Now I have one text field called City (to keep it simple) that I want 
to display/edit the City attribute of the Address entity related to 
the currently selected Person. In the value binding for the text 
field I've got it bound to Address Controller with a Controller Key 
of selection and a model key path of "city". (I have tried using 
"content" for the Controller Key too, but it doesn't work.)

With the current set up, the text fields that are bound to direct 
attributes of Person work fine. They modify the respective attributes 
of the Person entity selected in the table view. Open/save works fine 
too. However, the text field for city is grayed out and says "No 
Selection" (if I change the Controller Key to "content" it becomes 
editable but it doesn't do anything ie. it doesn't affect the stored 
attributes of the currently selected Person's Address).

I *think* my problem is with bindings, but I suppose it could be with 
my Core Data model. I'm getting pretty frustrated trying to figure 
this out, so I wanted to ask about it.

Related mailsAuthorDate
mlCore Data and Bindings - Problem accessing attributes of related entity Andrew Madsen Jun 30, 17:52
mlRe: Core Data and Bindings - Problem accessing attributes of related entity George Orthwein Jun 30, 19:12
mlRe: Core Data and Bindings - Problem accessing attributes of related entity Andrew Madsen Jun 30, 21:05
mlRe: Core Data and Bindings - Problem accessing attributes of related entity mmalc crawford Jun 30, 23:15