{"id":11234,"date":"2015-05-15T12:03:26","date_gmt":"2015-05-15T16:03:26","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=11234"},"modified":"2015-05-15T12:07:16","modified_gmt":"2015-05-15T16:07:16","slug":"how-not-to-crash-1-kvo-and-manual-bindings","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/05\/15\/how-not-to-crash-1-kvo-and-manual-bindings\/","title":{"rendered":"How Not to Crash #1: KVO and Manual Bindings"},"content":{"rendered":"<p><a href=\"http:\/\/inessential.com\/2015\/05\/14\/how_not_to_crash_1_kvo_and_manual_bind\">Brent Simmons<\/a>:<\/p>\n<blockquote cite=\"http:\/\/inessential.com\/2015\/05\/14\/how_not_to_crash_1_kvo_and_manual_bind\">\n<p>Here&rsquo;s the problem: the binding retains the <code>toObject<\/code> object. Which means that the button effectively retains the controller. If the controller retains its button (it should), then there&rsquo;s a retain cycle. Neither will become zombies, but they could become abandoned.<\/p>\n<p>[&#8230;]<\/p>\n<p>The solution we came up with fixes the retain cycle <em>without<\/em> your having to remember to call an invalidate method and call it at the exact right time. It&rsquo;s safer code.<\/p>\n<p>[&#8230;]<\/p>\n<p>In theory, bindings and KVO are there to promote loose coupling, but in practice the coupling is often just as tight &mdash; if not tighter, in a sense &mdash; and harder to debug and get right.<\/p>\n<\/blockquote>\n<p>Like <a href=\"https:\/\/twitter.com\/bwebster\/status\/599057868478812160\">Brian Webster<\/a>, I don&rsquo;t recall this being a problem for me, perhaps because I tend to use a different structure. Simmons has a controller binding a button to one of the controller&rsquo;s own properties. I tend to have controllers that bind widgets to key paths of a separate object controller that holds the model object. The outer controller then owns this object controller. I don&rsquo;t claim that this is necessarily the best way to do it&mdash;I have not used bindings much.<\/p>","protected":false},"excerpt":{"rendered":"<p>Brent Simmons: Here&rsquo;s the problem: the binding retains the toObject object. Which means that the button effectively retains the controller. If the controller retains its button (it should), then there&rsquo;s a retain cycle. Neither will become zombies, but they could become abandoned. [&#8230;] The solution we came up with fixes the retain cycle without your [&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":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","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,69,199,275,30,571,71],"class_list":["post-11234","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-bug","tag-cocoa","tag-bindings","tag-keyvalueobserving","tag-mac","tag-memory-management","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11234","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=11234"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11234\/revisions"}],"predecessor-version":[{"id":11236,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11234\/revisions\/11236"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=11234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=11234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=11234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}