NSNotification 常见错误?
简化...
建筑物有一系列公寓对象。每间公寓都有一个当前租户。这些租户属于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关键点在这里:
如果没有
currentTennant
,那么您的代码实际上是这样做的:当您使用
nil
作为对象参数,您告诉NSNotificationCenter
您希望将所有PlumbingIssue
警报传递给此观察者。您需要做的是确保仅在拥有currentTennant
时才设置通知。如果您使用属性,setCurrentTennant:
可能是执行此操作的好地方。请确保在
currentTennant
更改时将自己作为观察者删除,并始终确保在释放对象时完全将其作为观察者删除(否则NSNotificationCenter
可能会尝试发布向已释放对象发出通知,这是一件非常糟糕的事情)。- [NSNotifcationCenter removeObserver:]
是最简单的方法。The key bit is here:
If there's no
currentTennant
, then your code is actually doing this:When you use
nil
as the object parameter, you tellNSNotificationCenter
you want allPlumbingIssue
alerts delivered to this observer. What you'll need to do is make sure that you set up the notification only when you have acurrentTennant
. 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 elseNSNotificationCenter
may try to post notifications to a deallocated object, which is a Very Bad Thing).- [NSNotifcationCenter removeObserver:]
is the easiest way to do that.