Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8875517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:59:34+00:00 2026-06-14T18:59:34+00:00

In relation to this question I was wondering if there is any generally accepted

  • 0

In relation to this question I was wondering if there is any generally accepted logic regarding when to use NSNotification, with an observer in your main thread, vs using GCD to dispatch work from a background thread to the main thread?

It seems that with a notification-observer setup you have to remember to tear down the observer when your view unloads but then you reliably ignore the notification, where as dispatching a job to the main thread may result in a block being executed when the view has been unloaded.

As such, it seems to me that notifications should provide improved app stability. I’m assuming that the dispatch option provides better performance from what I’ve read of GCD?

UPDATE:

I’m aware that notifications and dispatch can work happily together and in some cases, should be used together. I’m trying to find out if there are specific cases where one should/shouldn’t be used.

An example case: Why would I select the main thread to fire a notification from a dispatched block rather than just dispatching the receiving function on the main queue? (Obviously there would be some changes to the receiving function in the two cases, but the end result would seem to be the same).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:59:35+00:00Added an answer on June 14, 2026 at 6:59 pm

    The NSNotificationCenter and gcd & dispatch_get_main_queue() serve very different purposes. I don’t thing “vs” is truly applicable.

    NSNotificationCenter provides a way of de-coupling disparate parts of your application. For example the kReachabilityChangedNotification 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)

    gcd 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.

    Generally (almost always) the notifications are observed on the thread on which they are posted. With the notable exception of one piece of API…

    The one piece of API where these to concepts intersect is NSNotificationCenter‘s:

    addObserverForName:object:queue:usingBlock:
    

    This is essentially a convenient method for ensuring that a given notification is observed on a given thread. Although the “usingBlock” parameter gives away that behind the scenes it’s using gcd.

    Here is an example of its usage. Suppose somewhere in my code there is an NSTimer calling this method every second:

    -(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];
        });
    }
    

    I want to use the backgroundColorIsGettingBoringNotification 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 viewDidLoad in the following code:

    @implementation NoWayWillMyBackgroundBeBoringViewController {
        id _observer;
    }
    -(void)observeHeyNotification:(NSNotification *)note{
        static NSArray *rainbow = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&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
    

    The primary advantage of this API seems to be that your observing block will be called during the postNotification... call. If you used the standard API and implemented observeHeyNotification: like the following there would be no guarantee how long it would be before your dispatch block was executed:

    -(void)observeHeyNotification:(NSNotification *)note{
        dispatch_async(dispatch_get_main_queue(), ^{
            // Same stuff here...
        });
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As a corollary to this question I was wondering if there was good comparative
In relation to this question , I was wondering if anyone knows a javascript
In relation to this question ( Efficient hashCode() implementation ) I have one more
This is in relation to this question . The proposed answers involve adding a
In relation to this stackoverflow question , how would I go about creating my
My question is in relation this this answer. https://stackoverflow.com/a/8773953/1297775 I have read at many
This question has some relation to this post I made recently: Drag a UIView
In relation to this question: Remove unused references (!= "using") , I would like
In relation to this question on Using OpenGL extensions , what's the purpose of
This is in relation to this question I am hosting this WCF service in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.