{"id":42,"date":"2008-05-14T22:08:02","date_gmt":"2008-05-15T03:08:02","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/05\/14\/nsalert-sheets-threads-inexplicable-bugs\/"},"modified":"2008-12-26T19:08:34","modified_gmt":"2008-12-27T00:08:34","slug":"nsalert-sheets-threads-inexplicable-bugs","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/05\/14\/nsalert-sheets-threads-inexplicable-bugs\/","title":{"rendered":"NSAlert + Sheets + Threads = Inexplicable Bugs"},"content":{"rendered":"<p>UPDATED 2008-12-26: <a href=\"http:\/\/vgable.com\/blog\/2008\/12\/26\/always-update-the-view-from-the-main-thread\/\">in general, <em>all<\/em> AppKit code should be called on the main thread<\/a>.<\/p>\n<p><strong>Problem:<\/strong><br \/>\nWhen using an <code><a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Reference\/ApplicationKit\/Classes\/NSAlert_Class\/index.html\"><strong>NSAlert<\/strong><\/a><\/code> to display a <strong>sheet<\/strong> in a <strong>multi-threaded application<\/strong>, unexpected badness can happen.<\/p>\n<p>I was using<br \/>\n<a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Reference\/ApplicationKit\/Classes\/NSAlert_Class\/Reference\/Reference.html#\/\/apple_ref\/occ\/instm\/NSAlert\/beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:\"><code>beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:<\/code><\/a><br \/>\nTo display an <code><a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Reference\/ApplicationKit\/Classes\/NSAlert_Class\/index.html\">NSAlert<\/a><\/code> as a sheet.<\/p>\n<p>But when the sheet appeared, the window it was attached to disappeared and got into some weird broken state where it would appear iff the application was not frontmost.<\/p>\n<p>Fortunately, I remembered <a href=\"http:\/\/imlocation.wordpress.com\/2007\/10\/11\/nsalert-sheet-not-having-focus\/\">having encountered weirdness with <code>NSAlert<\/code> sheets before<\/a>.  The symptoms were different (previously the alert didn&#8217;t have focus), but the same solution still worked.<\/p>\n<p><strong>Solution:<\/strong> make sure the message to display the sheet is sent by the main thread.  To do this, put the call to <code>beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:<\/code> inside another method, <code>showMyAlert<\/code>, then use <code>performSelectorOnMainThread:withObject:waitUntilDone:<\/code> to make sure <code>showMyAlert<\/code> is called on the main thread.<\/p>\n<p><strong>Work around<\/strong> use <code>runModal<\/code> to display the alert as a modal dialog instead of a sheet.  <code>runModal<\/code> Does not appear to have any problems when called from other threads.<\/p>\n<p><del>Just like <a href=\"http:\/\/imlocation.wordpress.com\/2007\/10\/11\/nsalert-sheet-not-having-focus\/\">last time<\/a>:<\/p>\n<blockquote><p>The whole incident feels funny to me.  I suspect there may be some deeper issue at work that I am not aware of.  When I have time to investigate further I shall update this post.  Unfortunately I don&#8217;t have time to look into &#8216;solved&#8217; bugs today.<\/p><\/blockquote>\n<p><\/del><\/p>\n<p>UPDATED 2008-12-26: <a href=\"http:\/\/vgable.com\/blog\/2008\/12\/26\/always-update-the-view-from-the-main-thread\/\">in general, <em>all<\/em> AppKit code should be called on the main thread<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATED 2008-12-26: in general, all AppKit code should be called on the main thread. Problem: When using an NSAlert to display a sheet in a multi-threaded application, unexpected badness can happen. I was using beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo: To display an NSAlert as a sheet. But when the sheet appeared, the window it was attached to disappeared and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,6,38,3,5,4],"tags":[259,270,29],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-bug-bite","category-cocoa","category-interface-builder","category-macosx","category-objective-c","category-programming","tag-multi-core","tag-nsalert","tag-threads"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}