Search code examples
objective-ccocoacore-datanstableviewcocoa-bindings

Cocoa Bindings with Core Data not updating all columns


I'm having this strange issue where I have an NSArrayController bound to a managedObjectContext and using the array controller to populate an NSTableView. I can create an object and insert it and it will add the new object and update one of the columns bound to the controller but not the others! I know the fields are being set and bindings are working correctly (perhaps) because if I close the app and restart the app everything is correctly populated.

I'm not sure what I'm missing! How can I debug the bindings? From what I've found here: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdTroubleshooting.html#//apple_ref/doc/uid/TP40002320-SW6 it basically just says could be your controller or key value coding compliance.

Here is the code creating the entity:

NSManagedObjectContext* context = [[BugManager shared] managedObjectContext];
NSError* error;
Bug* newBug = [NSEntityDescription insertNewObjectForEntityForName:@"Bug" inManagedObjectContext:context];
[newBug setValuesForKeysWithDictionary:data];
[context save:&error];

I have verified that all the fields are set correctly and saved. I also tried to set the values explicitly to see if it was a key value observing issue to no avail.

This is the column that is updating correctly:

enter image description here

Here is the one that isn't updating correctly:

enter image description here

And the array controller:

enter image description here

I can rearrange the table columns and it still only updates one, the target field (the one it was updating previously). I just want the table to update all the columns correctly but it is being more difficult than expected!


Solution

  • After hours and hours of slamming my head into the wall I traced the problem to not how the binding was set up (as it was correct) but actually how my data was being created. In the form that I was using to populate the object I had a text field and a text view. The text field was working correctly but the text view wasn't. When I switched to try a text field for both it worked as well. Or if I hard coded a string literal the bindings updated correctly. The fix for me was to create an explicit NSString* from the text view. If you ask for it's string it was giving me back an NSBigMutableString for some reason setting the core data field with this didn't fire the notification.

    So I did this:

    NSString* description = [[NSString alloc] initWithString:[[[self descriptionField] textStorage] string]];
    

    I then use that string value for my entity and everything works as it should.