NSNotification 常见错误?

发布于 2024-09-01 09:50:57 字数 783 浏览 3 评论 0原文

简化...

建筑物有一系列公寓对象。每间公寓都有一个当前租户。这些租户属于 Person 类型。请注意,currentTenant 没有对公寓的引用,因此无法将信息发送回链上。

当租户遇到管道问题时,他会提出 NSNotification:

[nc postNotificationName:@"PlumbingIssue" object:self];

每个公寓仅观察来自其当前租户的通知(这是在公寓建造时设置的,在当前租户出现之前):

[nc addObserver:self selector:@selector(alertBuildingManager:) name:@"PlumbingIssue" object:[self currentTenant];

当公寓收到来自其自己的通知时currentTenant,它发送自己的通知“PlumberRequired”,以及 NSDictionary 中的公寓号和 currentTenant。

公寓会观察这些通知,这些通知将从任何公寓(或其他对象)获取:

[nc addObserver:self selector:@selector(callPlumber) name:@"PlumberRequired" object:nil];

我在这里可能犯了根本性错误吗?发生的情况是,公寓正在接收来自所有当前租户的通知,而不仅仅是它自己的通知。

抱歉,实际代码有点难以发布。只是想知道我对观察来自特定发件人的通知的理解是否存在差距?

A simplification...

A building has an array of apartment objects. Each apartment has a single currentTenant. These tenants are of type Person. Note that currentTenant doesn't have a reference to the apartment, so can't send information back up the chain.

When a tenant has a plumbing issue he raises an NSNotification:

[nc postNotificationName:@"PlumbingIssue" object:self];

Each Apartment observes notifications ONLY FROM it's own current tenant (this is set up when the apartment is built, before there is a current tenant):

[nc addObserver:self selector:@selector(alertBuildingManager:) name:@"PlumbingIssue" object:[self currentTenant];

When the apartment receives a notification from it's own currentTenant, it sends it's own notification, "PlumberRequired", along with the apartment number and the currentTenant in an NSDictionary.

Apartment observes these notifications, which it will take from any apartment (or other object):

[nc addObserver:self selector:@selector(callPlumber) name:@"PlumberRequired" object:nil];

Is there something I could be getting fundamentally wrong here? What's happening is that the apartment is receiving notifications from any and all currentTenants, rather than jus it's own.

Sorry that the actual code is a bit too unwieldy to post. Was just wondering if there's a gap in my understanding about observing notifications from a particular sender?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随梦而飞# 2024-09-08 09:50:57

关键点在这里:

每间公寓仅观察来自其当前租户的通知(这是在公寓建成时设置的,在有当前租户之前)

如果没有 currentTennant,那么您的代码实际上是这样做的:

[nc addObserver:self selector:@selector(alertBuildingManager:) name:@"PlumbingIssue" object:nil];

当您使用 nil 作为对象参数,您告诉 NSNotificationCenter 您希望将所有 PlumbingIssue 警报传递给此观察者。您需要做的是确保仅在拥有 currentTennant 时才设置通知。如果您使用属性,setCurrentTennant: 可能是执行此操作的好地方。

请确保在 currentTennant 更改时将自己作为观察者删除,并始终确保在释放对象时完全将其作为观察者删除(否则 NSNotificationCenter 可能会尝试发布向已释放对象发出通知,这是一件非常糟糕的事情)。 - [NSNotifcationCenter removeObserver:] 是最简单的方法。

The key bit is here:

Each Apartment observes notifications ONLY FROM it's own current tenant (this is set up when the apartment is built, before there is a current tenant)

If there's no currentTennant, then your code is actually doing this:

[nc addObserver:self selector:@selector(alertBuildingManager:) name:@"PlumbingIssue" object:nil];

When you use nil as the object parameter, you tell NSNotificationCenter you want all PlumbingIssue alerts delivered to this observer. What you'll need to do is make sure that you set up the notification only when you have a currentTennant. If you're using properties, setCurrentTennant: would probably be a good place to do this.

Do make sure to remove yourself as an observer when currentTennant changes, and always make sure to remove your object as an observer entirely when it's deallocated (or else NSNotificationCenter may try to post notifications to a deallocated object, which is a Very Bad Thing). - [NSNotifcationCenter removeObserver:] is the easiest way to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文