Re: A TableView Binding question
Re: A TableView Binding question
- Subject: Re: A TableView Binding question
- From: Kyle Sluder <email@hidden>
- Date: Wed, 23 Feb 2011 08:32:31 -0800
Bringing this back on-list.
On Wed, Feb 23, 2011 at 5:44 AM, Motti Shneor
<email@hidden> wrote:
> Its fairly simple. I need to present on the table a column with some
> information that is not directly available in the model. It is calculated
> from several attributes using some non trivial logic.
What you have is called a derived property. The typical way to
implement this is to create a custom subclass of NSManagedObject and
define a property. See how the fullName property is implemented in the
Departments-and-Employees tutorial:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NSPersistentDocumentTutorial/03_CustomClass/customClass.html#//apple_ref/doc/uid/TP40002831-SW4
> However, I must say that not being able to observe the changes of a set (or array) of values, is a major drawback to the mechanism. Indeed It seems a bit "heavy" to add all the items of an array to my observing list, but If I try to find another solution --- well --- they all come out at least as heavy. And one should not underestimate the comfort and elegance of such a solution.
This has been a longstanding shortcoming in KVO, but it makes (some)
sense if you think about it. If you call [anObject
addObserver:myObject forKeyPath:@"some.key.path"…], KVO has to observe
each component of the key path. It will do the equivalent of:
[[anObject valueForKeyPath:@"some.key"] addObserver:myObject
forKeyPath:@"path"…];
[[anObject valueForKeyPath:@"some"] addObserver:someInternalObserver
forKeyPath:@"key"…];
[anObject addObserver:someInternalObserver forKeyPath:@"some"…];
If one of these keys is a to-many relationship, -valueForKeyPath: will
produce an NSArray. These arrays are transient; it's not the array
that's important, it's the objects contained in the array. So KVO will
throw an exception when it attempts to call -addObserver:forKeyPath:
on an NSArray.
I filed rdar://problem/9041056 last night asking for additional API to
make observing through to-many properties possible. KVO needs to
figure out or be told that a certain key is a to-many property; if the
property is implemented using the to-many collection accessors, it
already has this information. Apple could add API to inform Core Data
that a property implemented with the simple -foo/-setFoo: pair is a
to-many collection, and that would give it enough info to know how to
automatically observe through that keypath.
--Kyle Sluder
_______________________________________________
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