{"id":8727,"date":"2014-04-22T17:08:12","date_gmt":"2014-04-22T21:08:12","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=8727"},"modified":"2014-04-23T10:20:48","modified_gmt":"2014-04-23T14:20:48","slug":"nsnotificationcenter-is-not-thread-safe","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/04\/22\/nsnotificationcenter-is-not-thread-safe\/","title":{"rendered":"NSNotificationCenter Is Not Thread-safe"},"content":{"rendered":"<p><a href=\"http:\/\/lapcatsoftware.com\/articles\/nsnotificationcenter-is-threadsafe-not.html\">Jeff Johnson<\/a>:<\/p>\n<blockquote cite=\"http:\/\/lapcatsoftware.com\/articles\/nsnotificationcenter-is-threadsafe-not.html\">\n<p>In fact, if your app has multiple threads, then you&rsquo;re almost certainly using <code>NSNotificationCenter<\/code> wrong.<\/p>\n<p>[&#8230;]<\/p>\n<p>The above code crashes reliably in the <code>NSLog<\/code> after <code>sleep<\/code>. Why? What we see here is that <code>removeObserver:<\/code> does not block until all notifications have been posted. The method can return while a notification is still executing on another thread. Thus, we have a race condition.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Jeff Johnson: In fact, if your app has multiple threads, then you&rsquo;re almost certainly using NSNotificationCenter wrong. [&#8230;] The above code crashes reliably in the NSLog after sleep. Why? What we see here is that removeObserver: does not block until all notifications have been posted. The method can return while a notification is still executing [&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":[69,800,54,71],"class_list":["post-8727","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-concurrency","tag-objective-c","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8727","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=8727"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8727\/revisions"}],"predecessor-version":[{"id":8735,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8727\/revisions\/8735"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=8727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=8727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=8727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}