KVO: NSKeyValueChangeKind is wrong when observing to-many relationship which depends on another to-many relationship

Originator:armadsen
Number:rdar://23419605 Date Originated:05-Nov-2015 03:16 PM
Status:Open Resolved:
Product:OS X Product Version:Mac OS X 10.11.1 (15B42)
Classification:Other Bug Reproducible:Always
 
I have two array properties. One is public, immutable, and readonly. The other is "private", mutable, and is used as the internal backing for the public property. I want KVO to work on the public property. The code looks very much like the class 'Foo' in the attached sample program.

The issue is that when `bar` is observed, the value of `NSKeyValueChangeKindKey` is always `NSKeyValueChangeSetting`, even when the notification was triggered by an insert or removal on `internalBar`. If `internalBar` is observed directly instead, `NSKeyValueChangeKindKey` is as expected (`NSKeyValueChangeInsertion` or `NSKeyValueChangeRemoval`), along with the expected array of indexes for `NSKeyValueChangeIndexesKey`.

I know the documentation says:
"You cannot set up dependencies on to-many relationships by implementing keyPathsForValuesAffectingValueForKey:. Instead, you must observe the appropriate attribute of each of the objects in the to-many collection and respond to changes in their values by updating the dependent key yourself."

However, that seems to imply that the problem is when value for the dependent key(Path) depends on properties of objects in the to-many relationship's array, rather than on the contents of the relationship/array itself. And of course, this does basically work, it's just the `NSKeyValueChangeKindKey` that's wrong (and the missing `NSKeyValueChangeIndexesKey`).

The attached program demonstrates this behavior. If keyPathToObserve is @"bars", the value for NSKeyValueChangeKindKey in the observation method is always 1. If keyPathToObserve is instead @"internalBars", the value for NSKeyValueChangeKindKey will be correct (2 or 3 depending on whether a value was added or removed).

Comments

Example program can be found here: https://gist.github.com/armadsen/74d68f8689a8af471fa4


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!