Re: How to observe and bind (KVO/KVB) to a value generated at runtime...
Re: How to observe and bind (KVO/KVB) to a value generated at runtime...
- Subject: Re: How to observe and bind (KVO/KVB) to a value generated at runtime...
- From: Camillo Lugaresi <email@hidden>
- Date: Sat, 25 Feb 2006 02:03:24 +0100
On 24/feb/06, at 23:35, email@hidden wrote:
Title says it all.
I need to provide a proxy object that is KVB/KVO compliant for a
key not yet known until its requested.
I've implemented a proxy object that pipes set/Key values for Key
value coding, but when I return a proxy object
from an array index accessor (countOfItems/ItemAtIndex etc. style)
the table view wants to bind its text values to a key
not yet known. So when my proxy gets a undefinedValueForKey message
it checks if the object it wraps has a certain
conditions that it can take the value for that key, so I can return
a value or also set it.
The problem is that the following message occurs when returning my
proxy:
[<NSKeyValueArray 0x541b080>
addObserver:forKeyPath:options:context:] is not supported. Key
path: order
So I'm wondering, since I cant "pipe" values back and forth to the
wrapped object when bindings are concerned, how can I allow the
table column or other item bind to the underlying (wrapped) value??
It is definitely possible to bind to properties that are accessed
using valueForUndefinedKey: and setValue:forUndefinedKey:.
As an example, the wrapped object always has a variable called
"value," so what ever is sent or retrieved to the proxy, even if
its unknown,
I still want it to bridge to the wrapped object....... should I
override addObserver:forKeyPath:options:context: in the proxy and
pass it
to the wrapped object???
I'm not sure if that's safe. Clients might get confused if they think
they're observing one object and start getting notifications from
another. It's probably better to implement
addObserver:forKeyPath:options:context:, removeObserver:forKeyPath:
and observeValueForKeyPath:ofObject:change:context: on the proxy so
that it registers/unregisters itself as an observed on the wrapped
object and passes on notifications.
Then again, the KVO contract requires you to notify observers before
and after a property is changed, and respecting that requirement in a
proxy object is not easy when KVO only gives you a single
notification (IMHO this can be considered a design flaw in the API,
but you have to deal with it).
It is much easier to make the wrapped object a property of the proxy,
and bind to it using a key path (eg proxy.object.value instead of
proxy.value).
Camillo
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden