On Oct 15, 2013, at 9:39 AM, Jens Alfke <[email protected]> wrote: > On Oct 15, 2013, at 12:53 AM, Charles Srstka <[email protected]> wrote: > >> I've found that it's far more efficient to recurse *up* the NSTreeNode >> hierarchy — e.g. start with the object you're looking for, then look at its >> parent, then its grandparent, etc. until you end up at the root. > > But I don’t have an NSTreeNode at all. I have a model object, and I’m trying > to find the tree node that corresponds to it. > > If I already had an NSTreeNode I would just get its indexPath property; > problem solved, no recursion needed.
Right, I wasn't very clear in that post. I'll try to be a little more specific: Suppose you've got a hierarchy that looks like this: A ___________________ | \ \ \ B___ C___ D___ E | \ \ | \ \ | \ \ | F G H I J K L M N O and you've got object "M". Using the traditional way, i.e. Shipley's code, you end up iterating through the whole tree until you find your "M" object. So in this case, we go through the tree nodes for A, B, F, G, H, C, I, J, K, D, L, and M. If your tree is really large, this can get expensive, especially if there are huge hierarchies rooted under B and C. What I do is to ask my model object for its parent, then for its parent's parent, and so on until I get to the root. So here, I'd get M, D, A. Then, I go to the root of the NSTreeNode hierarchy and do that backwards. So I start with A, the root, then check its children for the NSTreeNode that corresponds with D, then check its children for the NSTreeNode that corresponds with M. So here I only had to iterate through A and D's children, as opposed to Shipley's method where I also have to iterate through B and C's children, as well as any children that F, G, H, I, J, K, and L might have. Charles _______________________________________________ Cocoa-dev mailing list ([email protected]) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
