views:

41

answers:

2

As many of you may know, an NSTreeController bound to an outline view can display duplicates while presenting core data entities.

A temporary solution is to add 'parent == nil' to the predicates, but this only returns parent entities. If, for instance, a user is searching for a sub-entity, the requested sub-entity won't be displayed.

A (proposed) solution is to subclass NSTreeController and add a NSMutableSet variable, which keeps track of entities that are currently being displayed. This variable should be alloced on init, and released on dealloc.

When "fetchWithRequest:merge:error:" is called, the set should be emptied (I'm not sure whether this would be more efficient than releasing it and allocating it again). Everytime an entity is going to be added to display, check if the set contains it. If it doesn't, add it. Otherwise, find which is closer to the root (which is the subentity) and either skip it if its the subentity, or swap it with the previously included one.

I think there should be relatively little impact on performance (considering NSSet uses hashing). The problem I'm having is finding the correct method to override to add this behavior. Specifically, where NSTreeController processes fetched entities after "fetchWithRequest:merge:error:" is called.

If anyone has any ideas or feedback, let me know. Thanks ahead of time.

+1  A: 

Is it fair to say you're really looking for a way to filter the tree with a search term without losing the tree structure? The inherent problem (beyond forcing the tree controller to include the parent nodes of a search match) is that the parents may or may not actually match the search result, so it's confusing to display them.

I think yours is more a problem of UI, isn't it? In that case, the best approach (and one I've seen many well-known companies and independent developers take) is to display search results in a plain table. This way the results can be sorted by various attributes and you don't have to disable drag and drop in the outline view in search mode (to avoid the user trying to change the tree structure when only part of the tree is displayed out of context).

Joshua Nozzi
I suppose it is a UI problem, but I feel like it is more of an underlying fetching problem. What I'd really like to do is display all of the entity instances that match the predicate, except ones that are children of other instances already being displayed. (I want to make "smart" folders)I suppose I can limit the search to root instances. Can you recommend a way to maintain sorting between separate smart folders?
jp.rider63
A: 

Expanding on Joshua's answer, I was able to implement Search Functionality into my own NSOutlineView, however it was limited to the root/parent objects in the view.

I think (like Joshua said) if you wanted to filter all objects you would have to display the results in a NSTableView.

Joshua