Re: NSArrayController making copies of my objects?
Re: NSArrayController making copies of my objects?
- Subject: Re: NSArrayController making copies of my objects?
- From: Trygve Inda <email@hidden>
- Date: Mon, 18 Oct 2010 19:51:34 +0000
- Thread-topic: NSArrayController making copies of my objects?
> On Oct 18, 2010, at 04:43, Trygve Inda wrote:
>
>> I understand that the array returned by allValues is not mutable so the
>> NSArrayController may need to make a mutable copy of it, but the internal
>> objects are mutable... So why is it copying my objects?
>>
>> If I add the following just before the above two lines, then the objects
>> have the same address as expected.
>>
>> [MyController willChangeValueForKey:@"myData"];
>> [MyController didChangeValueForKey:@"myData"];
>>
>> So I gather this is forcing the NSArrayController to write it's
>> copied/cached values back to myData.
>
> No, NSArrayController doesn't cache anything (well, it may cache something or
> other internally, but that has no relevance to its public functionality), and
> I can't think of any behavior that would cause it to unprovokedly copy
> objects.
>
> The fact that issuing a KVO notification for the "myData" property "fixes" the
> problem most likely indicates that your code is changing the value of "myData"
> in a non-KVO compliant way, at some point. If the array controller has already
> bound to the old value at the time you do this, it won't know about the
> change.
>
> I hope you won't mind if I go on to say that I think your entire design is
> misguided. For a start, as was pointed out in a different thread recently, you
> *really* can't rely on indexing into [someDictionary allValues]. There's
> *nothing* in the API that guarantees you'll get the dictionary objects in the
> same order every time you invoke 'allValues', even if the dictionary hasn't
> changed. Thus, your entire design is founded on an invalid assumption.
>
> Further, as the discussion in a related thread seems to be trying to tell you,
> taking a shortcut by using naked NSMutableDictionary hierarchies is a lousy
> approach. Your data structure requirements are more complicated, and you'll
> likely do better (get to the solution faster) to create the class(es) needed
> to really encapsulate the behavior you want.
Thanks for this (all who have responded). I can see that binding to
@allValues isn't a great idea to get both an array and dict representing the
same objects. (Though I was never indexing into the array returned from
allValues).
I see that I have 3 "stages" of options here...
1. Have a property in MyDataController that is an NSMutableArray, bind the
NSArrayController to it and each element of the array is a 9-string-element
NSMutableDictionary.
2. Encapsulate the 9-string-element NSMutableDictionary into it's own Class
with 9 string properties. I'd probably add a method "dictionary" to retrieve
the properties as a dictionary for easy writing to disk in a plist.
3. Either subclass NSMutableArray or create a class that conforms to the
indexed to-many relationship accessor requirements which would then allow me
to internally store the classes (#2) or dicts (#1) in both an ordered array
as well as a keyed dictionary.
I think (hope?) this sums it up.
T.
_______________________________________________
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