KVO Fires Extra Times When Registering Dependent Keys
| Originator: | minhtnguyen23 | ||
| Number: | rdar://29441796 | Date Originated: | 30-Nov-2016 10:50 AM |
| Status: | Open | Resolved: | |
| Product: | iOS + SDK | Product Version: | |
| Classification: | Reproducible: | YES |
When I implement KVO, the KVO notifications will fire extra times under this situation: add 2 or more of the same observers to the same keyPath without context (nil) and 1 of the depending key is a keyPath of member property. When the depending keyPath is changed, the number of notifications being fire will be a multiplication of the number of said observers. Steps to Reproduce: 1. Create an NSObject class called Foo with these member properties: dynamic var fullName : String (can be computed or stored) dynamic var firstName : String = "Foo" dynamic var bar : Bar = Bar() 2. Create an NSObject class called Bar with these member properties: dynamic var lastName : String = "Bar" 3. Register depending keyPaths ["firstName", "bar.lastName"] to key "fullName" of Foo 4. Now in the init of Foo, add observers as follow: self.addObserver(self, forKeyPath: "fullName", options: NSKeyValueObservingOptions.new, context: nil) self.addObserver(self, forKeyPath: "fullName", options: NSKeyValueObservingOptions.new, context: nil) 5. Now trigger the notification by changing the value of "bar.lastName" to "Baz". Expected Results: There should be 2 notifications sent. Actual Results: There are 4 notification sent. Version: Xcode Version 8.1 (8B62)
Comments
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!