Reputation: 886
I am having two objects in my model: Person and Address. Person has a key name and a relationship to Address and Address has a key street.
I have designed the user interface to show a table with one column (Person.name) and two text fields with name and street.
I have created one NSArrayController with managedObjectContext set and ObjectController set to Entity and EntityName Person.
Running the app the table shows all added persons. The two text fields are connected to the Person-ArrayController via Bindings tab:
nameField with value: Bind To: Person / Controller Key: selected / Model Key Path: name
streetField with value: Bind To: Person / Controller Key: selected / Model Key Path: address.street
I am able to write something into name and it is stored persistently. I am also able to enter something into streetField but it is not stored.
Does anyone know why??
Upvotes: 0
Views: 719
Reputation: 3399
When the array controller creates the person object, something similar to below code happens:
(assume moc is the AppDelegate managedObjectContext,also assume Person is the sub-class of NSManangedObject):
Person *personObj = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:moc];
personObj.name = @"something"; //some dummy values
[personObj.address setValue:@"dummy address" forKey:@"street"];
If personObj.address
is logged , nil will be printed on the console.
This is because the personObj.address
is an Address
object which till now has not been instantiated. Consequently personObj.address.street
will be NULL
.
This is what happens in your case also.
if you change the above code to the following it will work fine.
Person *personObj = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:moc];
//create an Address object and assign it to personObj.address (setting the relationship)
personObj.address= [NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:moc];
personObj.name = @"something"; //some dummy values
[personObj.address setValue:@"dummy address" forKey:@"street"];
and this can't be done using binding.
In short streetField
bound to address.street
doesn't get any value because address
doesn't point to an instantiated object.
Upvotes: 1