Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>NSNotificationCenter</code> and <code>gcd</code> &amp; <code>dispatch_get_main_queue()</code> serve very different purposes. I don't thing "vs" is truly applicable. </p> <p><code>NSNotificationCenter</code> provides a way of de-coupling disparate parts of your application. For example the <code>kReachabilityChangedNotification</code> in Apple's Reachability sample code is posted to the notification center when the system network status changes. And in turn you can ask the notification center to call your selector/invocation so you can respond to such an event.(Think Air Raid Siren)</p> <p><code>gcd</code> on the other hand provides a quick way of assigning work to be done on a queue specified by you. It lets you tell the system the points at which your code can be dissected and processed separately to take advantage of multiple-threads and of multi-core CPUs.</p> <p>Generally (almost always) the notifications are observed on the thread on which they are posted. With the notable exception of one piece of API...</p> <p>The one piece of API where these to concepts intersect is <code>NSNotificationCenter</code>'s:</p> <pre><code>addObserverForName:object:queue:usingBlock: </code></pre> <p>This is essentially a convenient method for ensuring that a given notification is observed on a given thread. Although the "<code>usingBlock</code>" parameter gives away that behind the scenes it's using <code>gcd</code>.</p> <p>Here is an example of its usage. Suppose somewhere in my code there is an <code>NSTimer</code> calling this method every second:</p> <pre><code>-(void)timerTimedOut:(NSTimer *)timer{ dispatch_async(dispatch_get_global_queue(0, 0), ^{ // Ha! Gotcha this is on a background thread. [[NSNotificationCenter defaultCenter] postNotificationName:backgroundColorIsGettingBoringNotification object:nil]; }); } </code></pre> <p>I want to use the <code>backgroundColorIsGettingBoringNotification</code> as a signal to me to change my view controller's view's background color. But it's posted on a background thread. Well I can use the afore mentioned API to observe that only on the main thread. Note <code>viewDidLoad</code> in the following code:</p> <pre><code>@implementation NoWayWillMyBackgroundBeBoringViewController { id _observer; } -(void)observeHeyNotification:(NSNotification *)note{ static NSArray *rainbow = nil; static dispatch_once_t onceToken; dispatch_once(&amp;onceToken, ^{ rainbow = @[[UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor blueColor], [UIColor purpleColor]]; }); NSInteger colorIndex = [rainbow indexOfObject:self.view.backgroundColor]; colorIndex++; if (colorIndex == rainbow.count) colorIndex = 0; self.view.backgroundColor = [rainbow objectAtIndex:colorIndex]; } - (void)viewDidLoad{ [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; __weak PNE_ViewController *weakSelf = self; _observer = [[NSNotificationCenter defaultCenter] addObserverForName:backgroundColorIsGettingBoringNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){ [weakSelf observeHeyNotification:note]; }]; } -(void)viewDidUnload{ [super viewDidUnload]; [[NSNotificationCenter defaultCenter] removeObserver:_observer]; } -(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:_observer]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end </code></pre> <p>The primary advantage of this API seems to be that your observing block will be called during the <code>postNotification...</code> call. If you used the standard API and implemented <code>observeHeyNotification:</code> like the following there would be no guarantee how long it would be before your dispatch block was executed:</p> <pre><code>-(void)observeHeyNotification:(NSNotification *)note{ dispatch_async(dispatch_get_main_queue(), ^{ // Same stuff here... }); } </code></pre> <p>Of course in this example you could simply not post the notification on a background thread, but this might come in handy if you are using a framework which makes no guarantees about on which thread it will post notifications.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload