Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop the Observer in NSNotification to called twice?

I have an observer of NSNotification which is called twice. I do not know what to do with it.

I googled it but no solution found.

[[NSNotificationCenter defaultCenter] addObserver:self      selector:@selector(connectedToServer:) name:@"ConnectedToServer" object:nil];  - (void)connectedToServer:(NSNotification*)notification {      [[NSNotificationCenter defaultCenter] postNotificationName:@"SendMessageToServer" object:message]; } 
like image 287
Azhar Avatar asked Oct 13 '11 08:10

Azhar


People also ask

How do I get rid of observers in Swift?

Use the method deinit . In Objective-C classes I would always remove NSNotificationCenter observers in the -dealloc method, but a Swift class doesn't have a -dealloc method. Instead, Swift has a deinit method.

How do I add alerts to observer?

First, register an observer for a notification with: addObserver(_:selector:name:object:) Then, post a notification with post(name:object:userInfo:) … … after which your selector is called. And don't forget to remove the observer with removeObserver()


2 Answers

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]; 
like image 118
EmptyStack Avatar answered Oct 05 '22 11:10

EmptyStack


If your addObserver method is run multiple times, it will create multiple observers. My issue was that I somehow placed my observer in viewWillAppear which appeared multiple times before I posted the notification and it resulted in my observer being called multiple times.

While EmptyStack's 3rd solution works, there is a reason your observer is being called twice, so by finding it, you can prevent needless lines of code instead of removing and adding the same observer.

I would suggest putting your observer in viewDidLoad to avoid simple errors like the one I experienced.

like image 32
tfrank377 Avatar answered Oct 05 '22 12:10

tfrank377