> On Oct 28, 2016, at 10:10 AM, Alexander Ljungberg <
stil...@gmail.com> wrote:
>
> Hi,
>
> Normally if your subclass listens for observations too, it would call the super class observation method as needed.
>
> You can also just use a delegate. Create a special observer instance whose only job is to receive the notifications.
Certainly, my workaround is to avoid duplicate observation and pass the method upstream.
> I think your use case sounds a little too abstract for me. Maybe if you had a more specific example it would be easier to reason about.
FWIW, I will give a specific use case that explains my situation, using a more generally understood milieu than my specific application:
Say we have a UI where we allow a user to edit their “profile” which includes various personal information. We need to watch all of the fields for changes so we can, among other things. prompt the user to save changes if they close the window or navigate away without executing an explicit save. Since this application does quite a bit of this kind of UI for different kinds of data sets, in typical OOP style this watch-and-check-save behavior is abstracted to a base class, but we have a specific subclass to manage the specifics for the “profile” data set. Specifically for this “profile” data set, there is a popup button, “preferred method of contact”, with the options: “Phone Call”, “Email Message”, and “Snail Mail”. The profile class has to watch the methodOfContactValue for a change, and present a different data entry UI. So the subclass wants to observe changes to the data value, but the super class also has to watch changes so it can determine an “edited” state for the UI.
In the Cocoa world, each implementation would use a different contextInfo value, and so (at least) receive multiple notifications, each observeValue implementation checking the contextInfo to see if the change concerns it and then acting upon it as necessary.
> That said if you’re right that Cocoa allows duplicate observers, that might warrant a change to Cappuccino. But we don’t necessarily copy Cocoa bug for bug – if it looks unintentional or we can’t understand what’s going we don’t copy that feature. We’re not a cargo cult. :)
I reality the bugs are due to developers not using the API correctly, so that is less an issue. Apple’s documentation o KVO and the use of contextInfo addresses my use case:
"The context pointer in the addObserver:forKeyPath:options:context: message contains arbitrary data that will be passed back to the observer in the corresponding change notifications. You may specify NULL and rely entirely on the key path string to determine the origin of a change notification, but this approach may cause problems for an object whose superclass is also observing the same key path for different reasons.
A safer and more extensible approach is to use the context to ensure notifications you receive are destined for your observer and not a superclass.”
I can’t think of a use case (and can think of undesired side-effects) for sending one notification for every addObserver call, but at least one call per unique observer, key path, and contextInfo would be best. The former, however, would likely be easier to implement, especially taking into account null or undefined contextInfo values.
For now, I will just put raising an exception on my list of fixes. I may look at it later to see if I can make sense of how KVO is implemented, and see what the best approach will be.