{"id":23240,"date":"2018-10-29T16:30:36","date_gmt":"2018-10-29T20:30:36","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=23240"},"modified":"2018-10-29T16:30:36","modified_gmt":"2018-10-29T20:30:36","slug":"nskeyvalueobservingcustomization-is-fundamentally-broken","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/10\/29\/nskeyvalueobservingcustomization-is-fundamentally-broken\/","title":{"rendered":"NSKeyValueObservingCustomization Is Fundamentally Broken"},"content":{"rendered":"<p><a href=\"https:\/\/bugs.swift.org\/browse\/SR-9081\">Lily Ballard<\/a> (<a href=\"https:\/\/twitter.com\/LilyInTech\/status\/1056428020959870977\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/bugs.swift.org\/browse\/SR-9081\"><p>The breakage is the fact that implementing it relies on being able to do equality testing on key paths, but equality testing on key paths doesn&rsquo;t work in the face of subclassing. By that I mean given the following code<\/p>\n<pre>class Foo: NSObject {\n    @objc dynamic var name: String?\n}\nclass Bar: Foo {}<\/pre>\n<p>The expression <code>\\Foo.name == \\Bar.name<\/code> returns <code>false<\/code> even though <code>Bar<\/code> is just inheriting its property from <code>Foo<\/code>. This means that an implementation of <code>NSKeyValueObservingCustomization<\/code> cannot possibly work properly on anything besides a non-<code>final<\/code> class. Even if keypath construction in this instance were changed such that <code>\\Bar.name<\/code> returns the same thing as <code>\\Foo.name<\/code>, the same cannot be done for the more complicated case[&#8230;]<\/p><\/blockquote>\n<p>I guess the workaround is to use the string-based Objective-C methods instead of the <code>AnyKeyPath<\/code> Swift ones.<\/p>\n\n<p>See also: <a href=\"https:\/\/bugs.swift.org\/browse\/SR-9077\">SR-9077<\/a>.<\/p>\n<blockquote cite=\"https:\/\/bugs.swift.org\/browse\/SR-9077\"><p><code>NSKeyValueObservingCustomization<\/code> relies on a global table to map <code>String<\/code> key paths back into <code>AnyKeyPath<\/code> values. However, as the string keypath does not include the root type, this means that observing properties with the same name on separate objects will overwrite each other in the global table. In a single-threaded scenario this is acceptable as the <code>NSKeyValueObservingCustomization<\/code> methods are invoked synchronously when the observation is created, and the global table is populated immediately prior to creating the observation. However, in a multithreaded scenario, the global keypath table could be overwritten with a different keypath prior to invoking the <code>NSKeyValueObservingCustomization<\/code> method.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Lily Ballard (tweet): The breakage is the fact that implementing it relies on being able to do equality testing on key paths, but equality testing on key paths doesn&rsquo;t work in the face of subclassing. By that I mean given the following code class Foo: NSObject { @objc dynamic var name: String? } class Bar: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2018-10-29T20:30:38Z","apple_news_api_id":"853f6211-72ec-4aa0-8609-d66decb02687","apple_news_api_modified_at":"2018-10-29T20:30:39Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AhT9iEXLsSqCGCdZt7LAmhw","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[131,800,31,1610,275,30,1609,71,901],"class_list":["post-23240","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-bug","tag-concurrency","tag-ios","tag-ios-12","tag-keyvalueobserving","tag-mac","tag-macos-10-14","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=23240"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23240\/revisions"}],"predecessor-version":[{"id":23241,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23240\/revisions\/23241"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=23240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=23240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=23240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}