{"id":20123,"date":"2018-01-09T13:08:15","date_gmt":"2018-01-09T18:08:15","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=20123"},"modified":"2018-03-01T10:25:47","modified_gmt":"2018-03-01T15:25:47","slug":"unregistering-block-based-notificationcenter-observers","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/01\/09\/unregistering-block-based-notificationcenter-observers\/","title":{"rendered":"Unregistering Block-based NotificationCenter Observers"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/blog\/2018\/01\/notificationcenter-removeobserver\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2018\/01\/notificationcenter-removeobserver\/\">\n<p>Yes, you still have to unregister block-based observations manually (as of iOS 11.2). The documentation for <code>removeObserver(_:)<\/code> is at least misleading if not wrong.<\/p>\n<p>[&#8230;]<\/p>\n<p>When you do the unregistering in <code>deinit<\/code>, <strong>you must make sure not to capture <code>self<\/code> in your observer block.<\/strong> If you do, your <code>deinit<\/code> will never get called because the block retains <code>self<\/code> (preventing its destruction) and the notification center holds a strong reference to the block. Your object will live forever.<\/p>\n<p>[&#8230;]<\/p>\n<p>I suggest you write a small wrapper class for the observation token the notification center returns to you. The wrapper object stores the token and waits to be deallocated. Its only task is to call <code>removeObserver(_:)<\/code> in its own deinitializer[&#8230;]<\/p>\n<\/blockquote>\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2013\/11\/20\/nsnotificationcenter-with-blocks-considered-harmful\/\">NSNotificationCenter With Blocks Considered Harmful<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: Yes, you still have to unregister block-based observations manually (as of iOS 11.2). The documentation for removeObserver(_:) is at least misleading if not wrong. [&#8230;] When you do the unregistering in deinit, you must make sure not to capture self in your observer block. If you do, your deinit will never get called [&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,69,164,31,1472,30,1529,571,71,901],"class_list":["post-20123","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-cocoa","tag-documentation","tag-ios","tag-ios-11","tag-mac","tag-macos-10-13","tag-memory-management","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20123","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=20123"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20123\/revisions"}],"predecessor-version":[{"id":20752,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20123\/revisions\/20752"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=20123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=20123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=20123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}