Re: NSTableView / NSArrayController Drawing
Re: NSTableView / NSArrayController Drawing
- Subject: Re: NSTableView / NSArrayController Drawing
- From: Ken Thomases <email@hidden>
- Date: Thu, 26 Jun 2008 22:54:10 -0500
On Jun 26, 2008, at 6:43 PM, Murray Bookchin wrote:
I'm using an NSArrayController to populate my NSTableView from an
array of
dictionaries. The problem is my app spends the majority of its cpu
time
re-drawing the NSTableview (~60-70%) as I add rows, even though the
view is
"idle" (displaying the top 20 or so rows which aren't changing).
I did find one thread in the archives on this topic... (
http://www.cocoabuilder.com/archive/message/cocoa/2007/4/24/182355) it
sounds like there's a trick to getting the ArrayController to
recognize that
the content array is mutable.. Doing that stops the ArrayController
from
redrawing the TableView every time something is added to the content
array?
The fix is not clear to me from that thread however...
Has anyone else experienced this and/or know how to work around it?
How are you modifying the array? I suspect you're using -
mutableArrayValueForKey:, and then mutating the returned array proxy.
Read the documentation for that.
In particular, note that if the mutable array primitive methods aren't
found, the proxy resorts to set<Key>:. Each time set<Key>: is used,
you're telling the receiver that the entire array is being replaced,
rather than making some more limited change to just one or a few
elements of the array. In particular, this is equivalent to calling
will/didChangeValueForKey:, which results in KVO notifications whose
NSKeyValueChangeKindKey has a value of NSKeyValueChangeSetting.
By contrast, if the mutable array primitive methods were available,
the proxy would use them. In turn, KVO could use the equivalent of
will/didChange:valuesAtIndexes:forKey:, which results in notifications
whose NSKeyValueChangeKindKey will be one of
NSKeyValueChangeInsertion, NSKeyValueChangeRemoval, or
NSKeyValueChangeReplacement.
Only the latter kinds of notifications give the NSTableView the
information it needs to perform efficient updates to itself.
If you're not using -mutableArrayValueForKey:, then you're either
calling set<Key>: yourself, which has the same effect, or you're
calling will/didChangeValueForKey: yourself. If you doing that, you
shouldn't. If you insist on generating the notifications manually,
use will/didChange:valuesAtIndexes:forKey:. Even better would be to
implement the mutable array primitive methods and use those. (Once
those are implemented, you _could_ use -mutableArrayValueForKey:, and
it would be able to be somewhat more efficient, but in that case it's
still an unnecessary intermediary and you'd be better off to just call
the mutable array primitive methods directly.)
So, to solve the issue you're seeing, I recommend that you implement
the mutable array primitive methods and use those when you need to
modify your array. <http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/AccessorConventions.html#//apple_ref/doc/uid/20002174-178830-BAJEDEFB
>
Cheers,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden