Re: Binding to object returned by custom getter

Jack Brindle

I think I would have a property that holds the velocity that is calculated in response to the keyPathsForValues… method being fired.
Then just bind to that property. Make sure that everything that is used to calculate velocity is included in the keyPathsForValues…
method or this won’t work very well.

It is permissible to use a method to do the calculating as it appears you have done, but you must call the willChange… and didChange…
methods at the bingeing and end of the calculation in order for the KVO to succeed. You really cannot call the method just to get the
value (it will fire the KVO, causing an infinite loop), which leads you right back to the method suggested above.

- Jack

On Jul 19, 2017, at 2:54 AM, Jonathan Taylor <jonathan.taylor@...> wrote:

Hi all,

I have a property "velocity" that returns a custom object representing a 2D vector. That vector object has properties "x" and "y". From time to time, the value of velocity to be displayed will change. Changes are deduced through keyPathsForValuesAffectingValueForKey logic (I'm just mentioning that in case it's relevant).

I have a custom getter for velocity, which returns an autoreleased vector object that I construct then and there, containing the current velocity value. I then have a UI element that is bound to velocity.x and .y. When the velocity to be displayed changes, I get the following error:

2017-07-19 10:43:37.336 StackViewer[15059:303] Cannot update for observer <NSAutounbinderObservance 0x19f217590> for the key path "velocity.y" from <PIVSliceViewer 0x10194de80>, most likely because the value for the key "velocity" has changed without an appropriate KVO notification being sent. Check the KVO-compliance of the PIVSliceViewer class.

I am a bit surprised at this - it had seemed to me that I was not doing anything unreasonable here, and I can't think of an obvious additional notification-related thing I should be adding to solve the problem. I am imagining it might have to do with the fact that I am returning a different (temporary) object for velocity, since I construct them on the fly in the getter - but that did not seem like an unreasonable thing to be doing.

Does this symptom sound like something people would expect or recognise? Any suggestions about what I am missing that could solve this? I guess I might be able to fix things by introducing an explicit backing variable for the velocity property, and updating that, but I think I'd prefer my current approach (which I think is simpler and cleaner) if I can work out what the problem is here...

Thanks for any suggestions

Join { to automatically receive all group messages.