2012-10-18 28 views
18

Nếu tôi có một số lớp học quan sát một NSNotification cụ thể, theo thứ tự các nhà quan sát được thông báo khi thông báo được đăng?Thứ tự thông báo NSNotification của thông báo quan sát

+2

Tôi tin rằng chúng được thông báo theo cùng một thứ tự so với khi bạn thêm người quan sát. Dù sao, tôi sẽ không dựa vào thực tế này bởi vì đây là về nội bộ của 'NSNotificationCenter' và có thể thay đổi trong tương lai. – atxe

Trả lời

18

Không bảo đảm về thông báo đơn đặt hàng nào được gửi đi. Nếu bạn cần một thứ tự bạn có thể muốn tạo một lớp mà lắng nghe một thông báo và gửi ra nhiều thông báo có thứ tự mà các lớp khác có thể nghe thay vào đó.

5

Đơn đặt hàng không được xác định. Apple quản lý danh sách các nhà quan sát và bất cứ khi nào thông báo được đăng, họ lặp lại trong danh sách và thông báo cho mọi người quan sát đã đăng ký. Danh sách có thể là một mảng hoặc một từ điển hoặc một cái gì đó hoàn toàn khác (ví dụ như một danh sách liên kết của các cấu trúc) và vì các nhà quan sát có thể được thêm vào và loại bỏ trong thời gian chạy bất cứ lúc nào, danh sách này cũng có thể thay đổi bất cứ lúc nào. danh sách hiện đang được triển khai, bạn không bao giờ có thể dựa vào bất kỳ thứ tự cụ thể nào. Hơn nữa bất kỳ bản cập nhật OS X nào cũng có thể làm cho danh sách nội bộ thay đổi và những gì giữ đúng cho 10.7 có thể không đúng với 10.8 hoặc 10.6.

0

Tôi đã thử nghiệm nó và nó trông giống như các đối tượng được sắp xếp theo addObserver phương pháp

Consol đầu ra cho thử nghiệm này là:

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 
Các vấn đề liên quan