objective-c - 发布两个 NSNotifications 时的执行顺序

标签 objective-c ios xcode nsnotifications nsnotificationcenter

一个关于 NSNotification 的快速问题...如果我在一个方法中发布两个 NSNotifications,并且它们被不同的对象观察到,那么执行的顺序是什么选择器方法?

例如,如果我有三个 Controller - Poster、Receiver A 和 Receiver B。在 Poster Controller 的函数中,我执行以下操作:

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

在接收器 A 的 viewDidLoad 方法中:

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

在接收器 B 的 viewDidLoad 方法中:

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

workToDoByA 会先完成然后是 workToDoByB 吗? 还是一起处决?

另一种情况...如果我有海报 A 发布通知,但同一通知有两个观察员。那么执行顺序是怎样的呢?

预先感谢您的帮助。

最佳答案

摘自 Apple docs :

A notification center delivers notifications to observers synchronously. In other words, the postNotification: methods do not return until all observers have received and processed the notification. To send notifications asynchronously use NSNotificationQueue. In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.

postNotificationName 的策略相同。

关于objective-c - 发布两个 NSNotifications 时的执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5164498/

相关文章:

xcode - 加载 View 时没有模拟器时iPad崩溃

xcode - 在方案选项中为应用程序语言添加更多选项

iphone - iOS - QuickLook - 如何在没有 UIScrollView 的情况下在 QuickLook 中打开对象

ios - 如何给UIButton添加属性?

objective-c - 在避免使用NSTimer的同时,如何实现“dayDidChange”方法?

javascript - 适用于 Ionic 应用程序的 Google Map API

ios - 如何使用Swift初始化AVAudioPlayer

swift - Xcode 11 - UITableViewAutomaticDimension 或 UITableView.automaticDimension 上的 Swift 编译器错误

ios - 继承和扩展框架类的问题

ios - 根据位置对 UIView subview z 顺序进行排序