r/macprogramming • u/metalcoholicfreak • Dec 07 '16
NSOutlineView + NSTreeController + itemForPersistentObject. Need some help.
I am writing a system Preference Pane where I show some items in a NSOutlineView whose contents are given by a NSTreeController. I need to store the status of each item (i.e. expanded or not) and after googling a lot I came to the conclusion that I need to implement itemForPersistentObject and persistentObjectForItem in my delegate class (among the other things).
I populate the TreeController via a function called populateOutlineView that I launch within mainViewDidLoad.
Since I'm working with NSTreeController the item that gets passed to persistentObjectForItem is a NSTreeNode, so I wrote:
- (id)outlineView:(NSOutlineView *)outlineView persistentObjectForItem:(nullable id)item
{
return [NSKeyedArchiver archivedDataWithRootObject:[(NSTreeNode*) item representedObject]];
}When reading a previously stored object via itemForPersistentObject I need to get the nodes of NSTreeController and compare each of them to the read object. I thought about writing something on the lines of:
- (id)outlineView:(NSOutlineView *)outlineView itemForPersistentObject:(id)object
{
NSArray *treeNodes = [self.myNodeTreeController arrangedObjects] childNodes];
MyNode *unarchivedObject = [NSKeyedUnarchiver unarchiveObjectWithData:object];
for (NSTreeNode* node in treeNodes){
MyNode *representedObject = [node representedObject];
if ([representedObject isEqual:unarchivedObject])
return node;
}
return nil; //should never reach here
}Now, the problem is that itemForPersistentObject gets called before MainViewDidLoad, hence before populateOutlineView, and it finds an empty NSTreeController.
Assuming I'm in the right direction, when should I add objects to NSTreeController so that they are ready by the time itemForPersistentObject needs them?
View all comments
Show parent comment
1
u/metalcoholicfreak Dec 09 '16
I am already using bindings between NSTreeController and NSOutlineView. I didn't want to use CoreData because I thought it would be too much for my needs. What I am doing instead is reading the items from a plist file and adding them to the Tree Controller via
addObjectmethod, then the OutlineView gets automatically updated via bindings (I don't add anything to it). Isn't that correct? Are you suggesting I should either use CoreData+bindings or plist file + NSOutlineViewDataSource (and get rid of the tree controller)?