{"id":29902,"date":"2020-08-26T16:26:45","date_gmt":"2020-08-26T20:26:45","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=29902"},"modified":"2020-08-28T15:33:16","modified_gmt":"2020-08-28T19:33:16","slug":"improved-kvo-in-big-sur","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2020\/08\/26\/improved-kvo-in-big-sur\/","title":{"rendered":"Improved KVO in Big Sur"},"content":{"rendered":"<p><a href=\"https:\/\/developer.apple.com\/documentation\/macos-release-notes\/macos-big-sur-11-beta-release-notes\">macOS Big Sur 11 beta 5<\/a>:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/documentation\/macos-release-notes\/macos-big-sur-11-beta-release-notes\"><p>Key-Value Observation removal facilities now employ deterministic bookkeeping methods. Cases that would have produced hard-to-diagnose crashes, especially those where KVO signals problems accessing deallocated observer pointers or observers associated with incorrect objects, now produce an exception pinpointing which observed object needs a missed <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/notificationcenter\/1413994-removeobserver\"><code>removeObserver(_:)<\/code><\/a> call, and by which observers. This exception was previously thrown as &lsquo;best effort&rsquo; when KVO could detect the problem; the new deterministic bookkeeping allows it to be thrown for all cases where <code>removeObserver(_:)<\/code> is needed.<\/p><p>The improved determinism also allows improved Swift API handling. Instances of <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/nskeyvalueobservation\"><code>NSKeyValueObservation<\/code><\/a>, produced by the Swift <code>NSObject.observe(_:changeHandler:)<\/code> method, take advantage of integration with this bookkeeping so they now invalidate automatically when the observed object is released,  regardless of how the object implements its KVO behavior. This applies to all usage of this API in macOS 11 Big Sur beta, including on processes built with previous versions of the SDK, and eliminates certain classes of crashes that sometimes required using the legacy API instead.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/07\/20\/swift-4-key-value-observation\/\">Swift 4: Key-Value Observation<\/a><\/li>\n<\/ul>\n\n<p id=\"improved-kvo-in-big-sur-update-2020-08-28\">Update (2020-08-28): <a href=\"https:\/\/twitter.com\/Catfish_Man\/status\/1299068301599862784\">David Smith<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/Catfish_Man\/status\/1299068301599862784\"><p>Tip re the <code>KVO_IS_RETAINING_ALL_OBSERVERS_OF_THIS_OBJECT_IF_IT_CRASHES_AN_OBSERVER_WAS_OVERRELEASED_OR_SMASHED<\/code> method that I added years ago: it really means <em>all<\/em> observers of that object, not just the ones being notified. Most of the time it&rsquo;s an unrelated one you forgot about.<\/p><p>(The reason is that determining which objects are being notified without introducing deadlocks requires dropping an internal lock, which would allow another thread to deallocate the observer out from under KVO, so it has to retain <em>then<\/em> figure out which are relevant)<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>macOS Big Sur 11 beta 5: Key-Value Observation removal facilities now employ deterministic bookkeeping methods. Cases that would have produced hard-to-diagnose crashes, especially those where KVO signals problems accessing deallocated observer pointers or observers associated with incorrect objects, now produce an exception pinpointing which observed object needs a missed removeObserver(_:) call, and by which observers. [&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":"2020-08-26T20:26:48Z","apple_news_api_id":"5f4a1775-2cf5-41b1-8b74-e7402f7fdb7f","apple_news_api_modified_at":"2020-08-28T19:33:19Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/AX0oXdSz1QbGLdOdAL3_bfw","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,275,30,1666,571,71,901],"class_list":["post-29902","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-keyvalueobserving","tag-mac","tag-macos-10-15","tag-memory-management","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/29902","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=29902"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/29902\/revisions"}],"predecessor-version":[{"id":29929,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/29902\/revisions\/29929"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=29902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=29902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=29902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}