Common NSNotification errors?

Simplification ...

The building has an array of apartment objects. Each apartment has one currentTenant. These tenants are of type Person. Note that currentTenant does not have an apartment reference, so it cannot send information to the chain.

When a tenant has a plumbing problem, they call NSNotification:

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

      

Each apartment is watching for notifications ONLY from its own tenant (this is set when the apartment is built, before there is a current tenant):

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

      

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

The apartment is watching these notifications, which they take from any apartment (or other object):

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

      

Is there something I might be getting fundamentally wrong here? What happens is that the apartment receives notifications from any and all existing Tenericas, and not from its own.

Sorry the actual code is too cumbersome to post. Was just wondering if there is a gap in my understanding about observing notifications from a specific sender?

+2


a source to share


1 answer


The key bit here:

Each apartment monitors notifications ONLY from its own tenant (this is set when the apartment is built, before there is a current tenant).

If not currentTennant

, then your code actually does this:



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

      

When you use nil

an object as a parameter, you NSNotificationCenter

are PlumbingIssue

signaling that you want all notifications to be delivered to this observer. You will need to make sure that you only set up a notification when you have one currentTennant

. If you are using properties it setCurrentTennant:

will probably be a good place to do this.

Be sure to dispose of yourself as an observer when currentTennant

changes, and always be sure to dispose of your object as an observer completely when it is freed (or NSNotificationCenter

might try to send notifications to the freed object, which is a very bad thing). - [NSNotifcationCenter removeObserver:]

is the easiest way to do it.

+3


a source







All Articles