Search code examples
macoscore-datacocoa-bindingsnsarraycontroller

Core Data not loaded into NSArrayController IB binding


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?


Solution

  • 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.