3

A quick question about NSNotification... If I post two NSNotifications in a method, and they are observed by different objects, what is the sequence of execution of the selector method?

For instance, if I have three controllers - Poster, Receiver A and Receiver B. In a function of the Poster controller, I do the following:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceiverADoSomething" object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceiverBDoSomething" object:self];

In the viewDidLoad method for receiver A:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(workToDoByA:) name:@"ReceiverADoSomething" object:nil];

In the viewDidLoad method for receiver B:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(workToDoByB:) name:@"ReceiverADoSomething" object:nil];

Will workToDoByA be done first followed by workToDoByB? Or they will be executed together?

Another scenario... If I have Poster A posting a notification but there are two observers to the SAME notification. What is the execution sequence then?

Thanks in advance for your help.

4

1 に答える 1

6

Apple ドキュメントからの抜粋:

通知センターは、オブザーバーに同期的に通知を配信します。つまり、postNotification: メソッドは、すべてのオブザーバーが通知を受信して​​処理するまで戻りません。通知を非同期に送信するには、NSNotificationQueue を使用します。マルチスレッド アプリケーションでは、通知は常に、通知が投稿されたスレッドで配信されます。これは、オブザーバーが自身を登録したスレッドと同じではない場合があります。

同じポリシーが postNotificationName にも適用されます。

于 2011-09-27T11:42:17.573 に答える