{"id":12094,"date":"2015-08-26T20:48:58","date_gmt":"2015-08-27T00:48:58","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12094"},"modified":"2015-08-26T20:50:49","modified_gmt":"2015-08-27T00:50:49","slug":"safer-block-based-nsnotificationcenter-api","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/08\/26\/safer-block-based-nsnotificationcenter-api\/","title":{"rendered":"Safer Block-based NSNotificationCenter API"},"content":{"rendered":"<p><a href=\"http:\/\/macoscope.com\/blog\/improving-notification-center\/\">Arkadiusz Holko<\/a>:<\/p>\n<blockquote cite=\"http:\/\/macoscope.com\/blog\/improving-notification-center\/\">\n<p>I wanted to learn how Clang decides when to show a warning, so I did what any reasonable person would do and dove into its source. Retain cycle checking is performed during the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Semantic_analysis_(compilers)\">semantic analysis phase<\/a>. <a href=\"https:\/\/github.com\/llvm-mirror\/clang\/blob\/6548e112229b523c0289c737dabb8a1481d047ed\/lib\/Sema\/SemaChecking.cpp#L8958-L9004\"><code>Sema::checkRetainCycles<\/code><\/a> is an overloaded method responsible for these checks.<\/p>\n<p>[&#8230;]<\/p>\n<p>In case of a message send, the compiler checks for retain cycles only when a selector looks like a setter, i.e., <a href=\"https:\/\/github.com\/llvm-mirror\/clang\/blob\/6548e112229b523c0289c737dabb8a1481d047ed\/lib\/Sema\/SemaChecking.cpp#L8706-L8726\">it starts with the word <code>add<\/code> or <code>set<\/code><\/a>. This check is rather simplistic. A warning isn&rsquo;t presented to the user when using <code>NSNotificationCenter<\/code>, because the compiler doesn&rsquo;t know the lifecycle of that object.<\/p>\n<\/blockquote>\n<p>So he made a <a href=\"https:\/\/github.com\/macoscope\/NotificationController\">MCSNotificationController<\/a> wrapper so that the compiler can detect the retain cycle. It also makes sure that you don&rsquo;t register for the same notification more than once and automatically unregisters in <code>-dealloc<\/code>.<\/p>\n<p>Previously: <a href=\"http:\/\/mjtsai.com\/blog\/2013\/11\/20\/nsnotificationcenter-with-blocks-considered-harmful\/\">NSNotificationCenter With Blocks Considered Harmful<\/a>, <a href=\"http:\/\/mjtsai.com\/blog\/2015\/05\/23\/how-not-to-crash-3-nsnotification\/\">How Not to Crash #3: NSNotification<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Arkadiusz Holko: I wanted to learn how Clang decides when to show a warning, so I did what any reasonable person would do and dove into its source. Retain cycle checking is performed during the semantic analysis phase. Sema::checkRetainCycles is an overloaded method responsible for these checks. [&#8230;] In case of a message send, the [&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":[55,168,230,69,255,31,30,571,54,74,71],"class_list":["post-12094","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-blocks","tag-clang","tag-cocoa","tag-compiler","tag-ios","tag-mac","tag-memory-management","tag-objective-c","tag-opensource","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12094","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=12094"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12094\/revisions"}],"predecessor-version":[{"id":12096,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12094\/revisions\/12096"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}