How to Avoid Adding Multiple Nsnotification Observer

How to avoid adding multiple NSNotification observer?

One way to prevent duplicate observers from being added is to explicitly call removeObserver for the target / selector before adding it again. I imagine you can add this as a category method:

@interface NSNotificationCenter (UniqueNotif)

- (void)addUniqueObserver:(id)observer selector:(SEL)selector name:(NSString *)name object:(id)object {

[[NSNotificationCenter defaultCenter] removeObserver:observer name:name object:object];
[[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:name object:object];

}

@end

This assumes that the you will only add one unique observer to each target for any notification name, as it will remove any existing observers for that notification name.

How to stop the Observer in NSNotification to called twice?

Solution 1: The first thing is to check if the notification itself is posted twice.

Solution 2: Even if the notification is posted only once, the action will be called as many times you've added the observer for the notification (no matter the notification is same or not). For example, the following two lines will register the observer(self) for the same notification(aSelector) twice.

[[NSNotificationCenter defaultCenter] addObserver:self selector:aSelector name:aName object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:aSelector name:aName object:nil];

You have to find where you are adding observer for the second time, and remove it. And also make sure that the code where you are add the observer is not called twice.

Solution 3: If you are not sure whether you have already added the observer or not, you can simply do the following. This will make sure that the observer is added only once.

[[NSNotificationCenter defaultCenter] removeObserver:self name:aName object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:aSelector name:aName object:nil];

Why is my NSNotification its observer called multiple times?

Based on this description, a likely cause is that your viewcontrollers are over-retained and not released when you think they are. This is quite common even with ARC if things are over-retained. So, you think that you have only one instance of a given viewcontroller active, whereas you actually have several live instances, and they all listen to the notifications.

If I was in this situation, I would put a breakpoint in the viewcontroller’s dealloc method and make sure it is deallocated correctly, if that’s the intended design of your app.

NSNotification multiple observers, only one gets called

In the code below you're posting before observing

 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"someName"
object:nil
userInfo:someUserInfo];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someMethod:)
name:@"someName"
object:nil];
}

* Original *
Plenty of possible issues here:

one the notification Name could be spelt wrong = @"someName" - If this is going to be observe red from other classes, think about creating an

in a .h (that both classes import)
include

extern NSString * const XXXSomeNameForSomeNotification; 

in the corresponding .m

NSString * const XXXSomeNameForSomeNotification = @"someName";

That's one way to ensure they're all observing/posting the right notification

If that's not your issue then try adding observing the notification from the viewDidLoad or another method that's called prior to viewDidAppear as it could be that it's not observing you notification when it's actually posted. Add break points to observe this.

Observer on NSNotificationCenter that can handle multiple notifications

//create contstant strings

#define kUpdatePictureNotification @"UpdatePictureNotification"
#define kDeletePictureNotification @"DeletePictureNotification"
#define kReasonForNotification @"ReasonForNotification"
#define kPictureNotification @"PictureNotification"

// to post a notfication call this method and give the reason either kUpdatePictureNotification or kDeletePictureNotification

-(void)postNotificationGivenReason:(NSString *)reason
{
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
reason, kReasonForNotification,
// if you need more stuff add more
nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kPictureNotification object:nil userInfo:dict];

}

// Here is the observer:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pictureNotification:) name:kPictureNotification object:nil];

// here is the action method for pictureNotification

-(void)pictureNotification:(NSNotification *)aNotification
{
NSString *reason = [aNotification.userInfo objectForKey:kReasonForNotification];
if ([reason isEqualToString:kUpdatePictureNotification])
{
// It was a UpdatePictureNotification
}
else
{
// It was a DeletePictureNotification
}
}


Related Topics



Leave a reply



Submit