NSNotification on iphone

I am sending NSSNotifcation to another view controller in the iPhone application, but its observer method is notified twice as much as possible, can someone help me.

I use this code to send notifications

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateStatusOnFacebook" object:nil userInfo:nil]; 

and added an observer

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postToWall) name:@"updateStatusOnFacebook" object:nil]; 
+4
source share
2 answers

Have you added an observer twice?

Which method do you call addObserver: selector: object: in? If it is in viewWillAppear, it can be called more than once.

Your method will be called as many times as you added the observer.

Try the following:

 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateStatusOnFacebook" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postToWall) name:@"updateStatusOnFacebook" object:nil]; 

Another reason is that you can just send a notification twice :)

+6
source

I had the same problem and read this question, but could only find one call to add an observer anywhere in the project.

In our case, the observer was added twice, because the method , in which there was a line, was called twice.

Be sure to execute your code by breaking the addObserver:selector:name:object call to make sure that you don't have an unexpected additional way to make this call.

0
source

Source: https://habr.com/ru/post/1337320/


All Articles