{"id":10481,"date":"2015-01-13T11:59:18","date_gmt":"2015-01-13T16:59:18","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10481"},"modified":"2025-12-16T15:49:53","modified_gmt":"2025-12-16T20:49:53","slug":"dependent-key-paths","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/01\/13\/dependent-key-paths\/","title":{"rendered":"Dependent Key Paths"},"content":{"rendered":"<p><a href=\"http:\/\/devetc.org\/code\/2015\/01\/11\/dependent-key-paths.html\">Jonathon Mah<\/a>:<\/p>\n<blockquote cite=\"http:\/\/devetc.org\/code\/2015\/01\/11\/dependent-key-paths.html\"><p>If user defaults can change separately from this code (and you should probably assume it can), providing correct KVO change notifications can be accomplished by having the singleton observe <code>NSUserDefaults<\/code> with <code>NSKeyValueObservingOptionPrior<\/code>; posting <code>willChangeValueForKey:@\"mySetting\"<\/code> on the prior callback and <code>didChangeValueForKey:@\"mySetting\"<\/code> on the post callback.\nBut this sucks &mdash; it&rsquo;s wordy and you pay a (small) performance cost even if <code>mySetting<\/code> isn&rsquo;t observed.<\/p><p>[&#8230;]<\/p><p>It&rsquo;s valid for dependencies to be specified in <em>either<\/em> the dispatching or specific methods, all the way up the inheritance chain.\nCorrectly overriding <code>+keyPathsForValuesAffectingValueForKey:<\/code> requires some tricky code, which I&rsquo;ve wrapped in a function <a href=\"https:\/\/github.com\/jmah\/MyLilKeyPathHelpers\/blob\/7ed5da8355090b27ceeeb02a20baa7bf7a63eb8a\/MyLilKeyPathHelpers\/MLHDependentKeyPaths.m#L41\">MLHOverrideKeyPathsForValueAffectingKey<\/a> in <a href=\"https:\/\/github.com\/jmah\/MyLilKeyPathHelpers\">MyLilKeyPathHelpers<\/a>.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Jonathon Mah: If user defaults can change separately from this code (and you should probably assume it can), providing correct KVO change notifications can be accomplished by having the singleton observe NSUserDefaults with NSKeyValueObservingOptionPrior; posting willChangeValueForKey:@\"mySetting\" on the prior callback and didChangeValueForKey:@\"mySetting\" on the post callback. But this sucks &mdash; it&rsquo;s wordy and you pay [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2025-12-16T20:49:55Z","apple_news_api_id":"7092aaa6-e757-46a5-bdac-e23257213c6a","apple_news_api_modified_at":"2025-12-16T20:49:55Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AcJKqpudXRqW9rOIyVyE8ag","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":[69,31,275,30,2868,54,71],"class_list":["post-10481","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-keyvalueobserving","tag-mac","tag-nsuserdefaults","tag-objective-c","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10481","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=10481"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10481\/revisions"}],"predecessor-version":[{"id":10483,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10481\/revisions\/10483"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}