<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2010-01-08-nsnotificationqueue.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Sun, 10 May 2026 04:45:57 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Hano - 2014-08-01 17:29:08</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>I'm also a bit curious if these warning still persists. How about iOS? </description><guid isPermaLink="true">6caa436445e7df67658bc6912f389e14</guid><pubDate>Fri, 01 Aug 2014 17:29:08 GMT</pubDate></item><item><title>folex - 2014-03-11 14:39:27</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>Are problems in the warning still persist?</description><guid isPermaLink="true">72caf3add9b7e9abe9abed453e7e8ac7</guid><pubDate>Tue, 11 Mar 2014 14:39:27 GMT</pubDate></item><item><title>Hoang Toan - 2012-04-11 04:52:59</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>Great post ! It's useful for me. Thanks so much.</description><guid isPermaLink="true">5abce9200ef0c8883a7d04fb4965bb90</guid><pubDate>Wed, 11 Apr 2012 04:52:59 GMT</pubDate></item><item><title>mikeash - 2010-02-14 21:51:15</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>Well, I don't think updating the class documentation would be enough here, although it certainly would have been better. If those warnings are correct then the class simply cannot be used safely. If that's the case, and they don't fix it, then they need to officially deprecate it to indicate that it's bad and you shouldn't touch it.</description><guid isPermaLink="true">a6478c036ba592f304e42b80166c0e48</guid><pubDate>Sun, 14 Feb 2010 21:51:15 GMT</pubDate></item><item><title>Jeremy W. Sherman - 2010-02-14 19:53:01</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>"Apple should have either deprecated the class or fixed it. Hiding the problem away in a release note is completely ridiculous."
&lt;br /&gt;
&lt;br /&gt;Far too much useful information is hidden away in release notes and never makes it into the permanent documentation. At the very least, they could have updated the &lt;code&gt;NSNotificationQueue&lt;/code&gt; class reference documentation to include the warning.
&lt;br /&gt;
&lt;br /&gt;The practical upshot of this is that the release notes are essential reading if you want to learn the fine and ugly details of the frameworks as they stand, rather than as they were documented four or five years ago. This, unfortunately, significantly reduces the utility of the documentation that you can readily access from Xcode: essential information is no longer just an Option-double-click away.</description><guid isPermaLink="true">196ce7d0cab5d2bd1dc1a40dffd94f1f</guid><pubDate>Sun, 14 Feb 2010 19:53:01 GMT</pubDate></item><item><title>mikeash - 2010-02-13 22:16:38</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>Well that's some Grade A BS right there. The described behavior makes the class completely useless, and therefore can only be considered a bug, even though it may align with an extremely legalistic and pathological reading of the documentation in question. Apple should have either deprecated the class or fixed it. Hiding the problem away in a release note is completely ridiculous.</description><guid isPermaLink="true">b1d9eba79d67d1251492a1e3c3a408ee</guid><pubDate>Sat, 13 Feb 2010 22:16:38 GMT</pubDate></item><item><title>Jeremy W. Sherman - 2010-02-13 19:25:21</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>You might want to be careful of &lt;code&gt;NSNotificationQueue&lt;/code&gt;. See the "Using NSNotificationQueue Warning" section in the Foundation release notes &amp;amp;lt;URL:&lt;a href="http://developer.apple.com/mac/library/releasenotes/Cocoa/Foundation.html&amp;amp;amp"&gt;http://developer.apple.com/mac/library/releasenotes/Cocoa/Foundation.html&amp;amp;&lt;/a&gt;;gt;. I've taken the liberty of quoting the passage in its entirety:
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;&lt;b&gt;Using NSNotificationQueue Warning (New since WWDC 2009)&lt;/b&gt;
&lt;br /&gt;NSNotificationQueue is an API and mechanism related to run loops (NSRunLoop), and the running of run loops.  In particular, posting of notifications via the notification queue is driven by the running of its associated run loop.  However, there is no definition for what run loop a notification queue uses, and there is no way for a client to configure that.  In fact, any given notification queue might be "tickled" into posting by nearly any run loop and different ones at different times (and given that enqueued notifications are also mode-specific, what mode(s) the run loop(s) are running in any any given time also come into play).  Further, although each notification queue is "thread-specific" (see the +defaultQueue documentation), there has never been any relationship between that thread and the run loop used by a notification queue.  This is all more and more problematic as more and more things move to happening on different and new threads.
&lt;br /&gt;
&lt;br /&gt;The practical upshot is:
&lt;br /&gt;- Notifications posted through a queue might not be posted in any particular "timely" fashion;
&lt;br /&gt;- Notifications posted through a queue might not ever be posted (the run loop of the thread that the queue chose to ask to poke it might not be run again; for example, the thread of that run loop might exit and the notification queue discarded);
&lt;br /&gt;- Notifications can be posted by any given queue on a different thread than the thread the queue is the default queue for (when a queue is a default queue for some thread);
&lt;br /&gt;- Notifications can be posted by any given queue on different threads over time;
&lt;br /&gt;- There is no necessary/guaranteed relationship between the thread(s) on which notifications are enqueued and those on which the notifications eventually get posted (delivered) for any notification queue
&lt;br /&gt;
&lt;br /&gt;This is true for all releases of Mac OS X.  Foundation and AppKit do not use NSNotificationQueue themselves, partly for these reasons.&lt;/div&gt;&lt;/div&gt;</description><guid isPermaLink="true">a463a9228074bd16e6b8c95c376b7993</guid><pubDate>Sat, 13 Feb 2010 19:25:21 GMT</pubDate></item><item><title>mikeash - 2010-01-12 06:21:45</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>It would be a great topic if it weren't for the fact that I don't know it well, and its great complexity doesn't translate to being all that interesting in terms of how it works. (Obviously it's interesting in terms of producing extremely useful results.) Thanks for the suggestion, though, I'll make a note of it in case I change my mind later.</description><guid isPermaLink="true">112672b94cb0ecd3a0445077f6ea8735</guid><pubDate>Tue, 12 Jan 2010 06:21:45 GMT</pubDate></item><item><title>Colin Barrett - 2010-01-10 21:50:16</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>Cocoa's text system is complicated, powerful and relatively poorly understood. Sounds like a great topic for Friday Q&amp;amp;A to me. </description><guid isPermaLink="true">39627c0c14b54ee83faf23ef1bc290d1</guid><pubDate>Sun, 10 Jan 2010 21:50:16 GMT</pubDate></item><item><title>mikeash - 2010-01-10 06:10:33</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>It would be pretty trivial to add a category to &lt;code&gt;NSNotificationCenter&lt;/code&gt; to add coalescing/delayed methods that just call through to &lt;code&gt;NSNotificationQueue&lt;/code&gt;, if you wanted to. Of course, if your grip is that &lt;code&gt;NSNotificationQueue&lt;/code&gt; is hard to discover, that doesn't help at all.</description><guid isPermaLink="true">5d26201381190a9471037696d6cd2b7d</guid><pubDate>Sun, 10 Jan 2010 06:10:33 GMT</pubDate></item><item><title>Frédéric Testuz - 2010-01-09 08:01:57</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>I used NSNotificationQueue some years ago. I had never used thread, had to process a large amount of data and wanted to keep my app responsive. But at this time, I didn't have the time to learn about threading issues. 
&lt;br /&gt;
&lt;br /&gt;So I break my code in small portions. At the end of a portion I sent a notification with NSPostWhenIdle. When the notification was post I proceed with the next portion. It worked very well.</description><guid isPermaLink="true">784f68f333288f6767a4ea7e512627d6</guid><pubDate>Sat, 09 Jan 2010 08:01:57 GMT</pubDate></item><item><title>John McLaughlin - 2010-01-09 06:12:47</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>Great Post -- Many a time I've wondered how to execute something lazily (and I just don't mean my personal life!)
&lt;br /&gt;
&lt;br /&gt;I sort of wish they hadn't split NSNotification and NSNotificationQueue but instead provided a single interface and allow flags or calling semantics.
&lt;br /&gt;
&lt;br /&gt;I'm sure I'll use soon!</description><guid isPermaLink="true">e6b908cdd8ac53c6cd430ac71329721d</guid><pubDate>Sat, 09 Jan 2010 06:12:47 GMT</pubDate></item><item><title>mikeash - 2010-01-09 03:59:16</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>Yes, I've found &lt;code&gt;-performSelectorOnMainThread:&lt;/code&gt; to be troublesome when used in a tight loop. It operates at a high priority and can cause the app to become unresponsive if you aren't careful.
&lt;br /&gt;
&lt;br /&gt;For #3, from the docs:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt; A notification queue maintains notifications (instances of NSNotification) generally in a first in first out (FIFO) order.&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;That word "generally" is a bit disconcerting, but I think that refers to the fact that e.g. NSPostASAP notifications will happen before NSPostWhenIdle even if the NSPostASAP happened later.
&lt;br /&gt;
&lt;br /&gt;I have not tested it, though, so it could be way wrong.</description><guid isPermaLink="true">74a13f963291acba41836229726f29c0</guid><pubDate>Sat, 09 Jan 2010 03:59:16 GMT</pubDate></item><item><title>Sarah Holbrook - 2010-01-08 18:26:17</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>Mike, very valid points. Posting to the main thread (#2 in your followup) is really helpful especially when the intent is for the UI/controller classes to act on the notification. It's another one to be careful with though; I've seen it lead to performance problems that aren't obvious when sampling.
&lt;br /&gt;
&lt;br /&gt;Adding one more notification (#3) seems like it could work. I'm curious if anyone's tested it. If there were another "idle" notification is it guaranteed to send first? I haven't verified if the queue is mostly in name or whether it's truly FIFO with delayed notifications.</description><guid isPermaLink="true">0cc4f7e6b81969cdbc65b55d21054d89</guid><pubDate>Fri, 08 Jan 2010 18:26:17 GMT</pubDate></item><item><title>Anonymous - 2010-01-08 17:50:15</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>Interesting you mention this class, I only noticed it for the first time a month ago when I was redoing The Gist (OSS Twitter app) to no longer use bindings to update other observers that new tweets were imported into the app from Twitter. This class makes perfect sense in this context because tweets can be imported individually or in large, indeterminately-sized batches.</description><guid isPermaLink="true">e6f59ce1c4bd71e673b47fc8cd97a9bc</guid><pubDate>Fri, 08 Jan 2010 17:50:15 GMT</pubDate></item><item><title>mikeash - 2010-01-08 15:58:54</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>&lt;b&gt;Daniel Hedrick:&lt;/b&gt; Yes, they would be in the slider's target. &lt;code&gt;sliderMoved:&lt;/code&gt; would be set as the target of the slider. You'd also need some code somewhere (in the init method, probably) to add the object as an observer for &lt;code&gt; SliderDoneMovingNotification&lt;/code&gt; with &lt;code&gt;sliderDoneMoving:&lt;/code&gt; as the observation method. There's no automatic hookup for that sort of thing, unfortunately.
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Sarah Holbrook:&lt;/b&gt; The problem of secondary threads with finite lifetimes is an interesting one. I can see a few reasonable solutions:
&lt;br /&gt;
&lt;br /&gt;1) Only use &lt;code&gt;NSNotificationQueue&lt;/code&gt; for things that can withstand not being delivered.
&lt;br /&gt;
&lt;br /&gt;2) Ship the queued notifications to the main thread, or to another thread which lives forever.
&lt;br /&gt;
&lt;br /&gt;3) When exiting the thread, add another notification with &lt;code&gt;NSPostWhenIdle&lt;/code&gt;, then spin the runloop until it fires. This should mean all other pending notifications have fired as well... assuming none of the observers use &lt;code&gt;NSNotificationQueue&lt;/code&gt;!
&lt;br /&gt;
&lt;br /&gt;Definitely takes some added thought and work for this case, though.
&lt;br /&gt;
&lt;br /&gt;Speaking of &lt;code&gt;NSOperation&lt;/code&gt;, using &lt;code&gt;NSNotificationQueue&lt;/code&gt; from an &lt;code&gt;NSOperationQueue&lt;/code&gt; or a GCD queue could be a really good way to run into trouble.</description><guid isPermaLink="true">f372dcd7c9d735e5ddc5bbfe5bb68f5c</guid><pubDate>Fri, 08 Jan 2010 15:58:54 GMT</pubDate></item><item><title>Sarah Holbrook - 2010-01-08 15:45:08</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>This is really helpful when running on the main event loop.
&lt;br /&gt;
&lt;br /&gt;For any thread that has a finite life, be careful when using anything but NSPostNow. The delayed notifications are not guaranteed to be sent. I don't know of an elegant solution; one workaround is to give the current runloop an extra workout before the thread dies. In practice I've seen delayed notifications on a background thread lead to many bugs when not managed carefully. 
&lt;br /&gt;
&lt;br /&gt;It depends on what you're trying to acheive but NSOperation dependencies offer an alternate for "do this afterwards" when you're off the event loop (but need to be configured before the initial work begins).</description><guid isPermaLink="true">d9deeff632e61840d9113d73a08abd20</guid><pubDate>Fri, 08 Jan 2010 15:45:08 GMT</pubDate></item><item><title>Dan Leehr - 2010-01-08 15:38:55</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>Great post, an under-covered subject that had my gears turning the whole time.  I use notifications very heavily in my iPhone app and I can think of several places where I &lt;b&gt;should&lt;/b&gt; be using  notification queues to coalesce them.  
&lt;br /&gt;Also, this solidifies the run loop concepts even more, so bonus!</description><guid isPermaLink="true">e8568d5dd8cde65d1df372573756ed2e</guid><pubDate>Fri, 08 Jan 2010 15:38:55 GMT</pubDate></item><item><title>Daniel Hedrick - 2010-01-08 15:27:31</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>Quick question about these two methods you're defining (mostly about semantics, I guess)...
&lt;br /&gt;
&lt;br /&gt;Would these be declared in the controller that is the target of the slider?
&lt;br /&gt;
&lt;br /&gt;I'm assuming so, but I'm a little rusty on my Cocoa and not in front of a Mac to test (for shame!).</description><guid isPermaLink="true">cf5b1297b2cb5d21f3af79aabf84073c</guid><pubDate>Fri, 08 Jan 2010 15:27:31 GMT</pubDate></item><item><title>mikeash - 2010-01-08 15:10:50</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>I wondered many times while writing this post whether other people would find it interesting, or if I was the only one who missed the significance of this class for so long. Looks like I was right to go ahead!
&lt;br /&gt;
&lt;br /&gt;If you can find any more weird, obscure, useful classes to expound upon, let me know!</description><guid isPermaLink="true">6ee8fccc58dd6e625200eaeb10a76bb1</guid><pubDate>Fri, 08 Jan 2010 15:10:50 GMT</pubDate></item><item><title>Jonathan Wight - 2010-01-08 14:18:35</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html#comments</link><description>Crap. I've written code that basically does everything that NSNotificationQueue is doing. I should have just used NSNotificationQueue - now I know to use it in future.
&lt;br /&gt;
&lt;br /&gt;It's definitely one of those classes that you look at in the docs and think "that's useful, wonder what I should use it for", and then you promptly forget about it.
&lt;br /&gt;
&lt;br /&gt;More topics like this please! (And the NSRunloop post was great too). I'll scan through Foundation.framework and look for other obscure but useful classes.</description><guid isPermaLink="true">0c0d1310675dbf2de8194a150169525c</guid><pubDate>Fri, 08 Jan 2010 14:18:35 GMT</pubDate></item></channel></rss>
