Common NSNotification mistakes?

Posted by Ben Packard on Stack Overflow See other posts from Stack Overflow or by Ben Packard
Published on 2010-05-13T13:52:13Z Indexed on 2010/05/13 14:04 UTC
Read the original article Hit count: 291

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?

© Stack Overflow or respective owner

Related posts about nsnotifications

Related posts about cocoa