Re: NSOutlineView Problem
Re: NSOutlineView Problem
- Subject: Re: NSOutlineView Problem
- From: John Terranova <email@hidden>
- Date: Wed, 9 Jun 2004 22:29:53 -0700
On Jun 9, 2004, at 9:01 PM, Scott Ahten wrote:
On Jun 9, 2004, at 4:44 PM, Andreas Mayer wrote:
Am 09.06.2004 um 21:08 schrieb Scott Ahten:
When is it safe to release the objects returned to an NSOutlineView
in outlineView:child:ofItem?
When you don't need the outline view anymore?
If it was only that simple :)
My data source asks the item (XMLTree
http://iharder.net/macosx/xmltree/ ) for it's child at the requested
index and returns it. The object is created by the item and I pass it
on to the outline view when requested.
// Simplified version of the method...
- (id)outlineView:(NSOutlineView *)ov child:(int)index ofItem:(id)item
{
XMLTree * childTree;
// is the parent non-nil?
if (item) {
childTree = [item childAtIndex:index];
} else {
// Else return the root
childTree = [docTree childAtIndex:1];
}
return [childTree retain];
}
Looking at the source for childAtIndex, the item creates a new XMLTree
based on the child node (a CFXMLTreeRef) and returns it autoreleased.
Since the outline view calls this method multiple times for the same
child, multiple versions of same child are created.
This is your problem. You should already have created your objects
(XMLTree, CFXMLTreeRef, whatever) and you need to maintain these
objects in some data structure (e.g. array, tree, dictionary) yourself.
Then in - (id)outlineView:(NSOutlineView *)ov child:(int)index
ofItem:(id)item you just return your copy of the object.
Since you are returning an object you already own, the object should
not be going away anytime soon. The outlineview will neither retain
nor [auto]release the object as it is not going to hold onto the object
long enough to bother, rather it will just ask for the object again
every time it needs it. This is why your outlineview:child:ofItem:
routine must be fast (it should not be creating objects every time),
just accessing a data structure to grab an existing object and return
it.
Just as the outlineview neither retains nor [auto]releases the object,
you shouldn't either when you return the object to the outlineview.
It might make sense (depending on your data) to create the data lazily.
The first time the outlineview asks you for it, you create it and
return it. But, you also store the object in your own data structures,
so that the next time the outlineview asks for the object, you don't
have to create it, you just get it from your data structure.
--
john terranova, email@hidden
"You never know what you'll do
until you do what you do
when you're broke." -- Todd Snider, "Broke"
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.