If I have several classes observing a particular NSNotification, in what order are the observers notified when the notification is posted?
-
2I believe they are notified in the same order than you add the observer. Anyway, I would not rely on this fact because this is about the internals of `NSNotificationCenter` and can change in the future. – atxe Oct 18 '12 at 14:59
3 Answers
There is no guarantee as to what order notifications are sent out. If you need an ordering you may want to create a class that listens for one notification and sends out multiple ordered notifications that other classes can listen for instead.

- 74,769
- 26
- 128
- 150
-
1How do you know there's no guarantee as to what order notifications are sent out? Is it written somewhere in Apple's documentation? – ma11hew28 Jun 22 '19 at 20:27
-
1@ma11hew28 There is nothing explicitly saying there is no order; neither is there anything saying there is an order (which is why I say there is no guarantee). Since there is no information either way you have to assume they may be unordered, even if currently they seem to behave every time as if they are ordered... – Kendall Helmstetter Gelner Jun 24 '19 at 04:47
-
The order is undefined. Apple manages a list of observers and whenever the notification is posted, they iterate over the list and notify every registered observer. The list may be an array or a dictionary or something completely different (e.g. a linked list of structs) and since observers can be added and removed at runtime at any time, the list may also change at any time, thus even if you knew how the list is currently implemented, you can never rely on any specific order. Further any OS X update may cause the list internals to change and what holds true for 10.7 may not hold true for 10.8 or 10.6.

- 125,244
- 33
- 244
- 253
-
1How do you know the order is undefined? Is it written somewhere in Apple's documentation? – ma11hew28 Jun 22 '19 at 20:26
-
@ma11hew28 This works the other way round: **Everything** is undefined unless it is defined somewhere. You don't have to explicitly undefine things, you have to explicitly define them. And as Apple has nowhere defined the order in the documentation, the order is automatically undefined. – Mecki Apr 21 '20 at 15:19
-
I have tested it and it looks like the objects are ordered by addObserver method
Consol output for this test is :
2016-04-04 22:04:02.627 notificationsTest[1910:763733] controller 8
2016-04-04 22:04:02.629 notificationsTest[1910:763733] controller 1
2016-04-04 22:04:02.629 notificationsTest[1910:763733] controller 2
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#include <stdlib.h>
@interface AppDelegate ()
@property (strong, readwrite, nonatomic) NSTimer *timer;
@property (strong, readwrite, nonatomic) NSMutableArray *array;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.array = [NSMutableArray array];
ViewController *vc3 = [ViewController new]; vc3.index = 8;
ViewController *vc1 = [ViewController new]; vc1.index = 1;
ViewController *vc2 = [ViewController new]; vc2.index = 2;
[self.array addObject:vc1];
[self.array addObject:vc3];
[self.array addObject:vc2];
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(sendNotification:) userInfo:nil repeats:YES];
return YES;
}
- (void)sendNotification:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTitle1 object:nil];
}
@end
ViewController.m
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
@property (assign, readwrite, nonatomic) NSInteger index;
@end
@implementation ViewController
- (instancetype)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondToNotification:) name:kNotificationTitle1 object:nil];
}
return self;
}
- (void)respondToNotification:(NSNotification *)notification {
NSLog(@"controller %ld", self.index);
}
@end

- 8,936
- 7
- 53
- 93