On Feb 11, 2010, at 10:33 PM, Quincey Morris wrote:

> There isn't a "replace" method for an array controller. If you must do it as 
> a single operation, then solution would be to fetch the original object at 
> the end of editing, update its properties to match the (edited) properties of 
> the copy, then discard the now-temporary object copy.
> 
> However, your description doesn't completely make sense. There are no objects 
> "in" an array controller. Instead, think of the array controller as a sorted, 
> filtered perspective on your underlying data. Normally, the array controller 
> is monitoring the underlying data via KVO, so any underlying changes are 
> noticed and it adjusts itself accordingly without your writing any code.
> 
> You seem to be saying that you've already updated the underlying data model 
> ("I have implemented all of the KVC methods in the Manager class ..."), in 
> which case you *don't* want to mess with the array controller directly. 
> Basically, there are two approaches to adding, deleting and replacing, when 
> an array controller is involved:
> 
> 1. Update the data model directly and KVO compliantly, and let the array 
> controller notice the changes via KVO.
> 
> 2. Do not update the data model directly, but use the array controller 
> methods instead (removeObject:, insertObject:atArrangedObjectIndex:, etc). 
> These methods internally cause the data model to be updated.
> 
> Pick one. Not both.
> 
> Method #1 is the cleanest, because it doesn't introduce the array controller 
> (which is really part of your UI glue code) into the data model update, but 
> you *must* ensure that the data model change is made KVO compliantly. 
> Typically, that means making the changes using a mutableArrayValueForKey: 
> proxy object.
> 
> Method #2 is a convenience when you've not built (or have not been able to 
> build) a clean MVC design into your app, or when you must work with the 
> "arrangedObjects" order for some reason.
> 
> Others will likely disagree with me, but I think that there's something a bit 
> "smelly" about code referring to array controllers. It often indicates a 
> defect in the MVC design.
> 

Quincey,

Your explanation took a little while to make sense to me, but I finally did 
have the "aha" moment. You were right that I was confusing my MVC design, 
trying to use a controller object to tell another controller object to change 
the model. I wasn't aware that I could just change the model directly and that 
KVO would affect the other controller for me. It still seems rather magical how 
it does that, but I've reworked my code to directly call my 
replaceObjectInMyArrayAtIndex:withObject: and verified that the changes are now 
flowing through to the UI just how I wanted.

Thank you for helping me through this learning experience; I'm sure I will draw 
from it again and again.

Bill


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to