1
votes

All,
I am at my wits end trying to figure this out. I have a NSViewController that contains a NSArrayController and a table whose dataSource is set to the view controller. The NSArrayController is setup in IB to contain Core Data entity objects. Prepares Content and Auto Rearrange Content are set to YES. The MOC binding is set. There are bindings for a filterPredicate and a sortDescriptor. When my NSViewController is loaded, the method numberOfRowsInTableView: is called, but my NSArrayController contains no data. After everything is displayed/loaded, the NSArrayController seems to contain the data (if I add another Entity object, all the objects are displayed). I've tried calling rearrangeObjects, but that doesn't seem to work. Any ideas?

1
Why are you using the datasource protocol? You should just bind the table to the array controller. You can try calling fetch:withRequest** on the AC--nothing is bound to its arrangedObjects, so it perhaps never fetched them-- but you're sort of missing the point of the while bindings thing if you don't got the last mile and bind the controller to the view. ** fetch defers until the next runloop iteration so it'd be useless in your datasource method, you need the withRequest version.stevesliva
The table's format does not match 1:1 to the Entity's data in this particular case. To make the data more readable to the user I have chosen to use the datasource protocol.Scott Henderson

1 Answers

2
votes

As you set managed object context in IB with help of binding the NSArrayController's fetch is executed as a delayed operation performed after its managed object context is set (by nib loading). This therefore happens after awakeFromNib: and windowControllerDidLoadNib:.

So when the view controller is loaded the method NumberOfRowsInTableView: is called before NSArrayController's managed object context property is set. Hence, at this moment NSArrayControoler returns zero.

When you reloadData for your tableView NumberOfRowsInTableView: is called again and at this moment NSArrayController contains data as its fetch has been already executed.