简体   繁体   中英

NSNotification being added multiple times

I have added an observer in viewDidLoad and the issues is if I pop back and come to the same class again, it adds observer multiple times.

Below is the code as how observer is being added:

- (void)viewDidLoad 
  {   
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PaymentRecieved" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(PaymentRecieved:)
                                            name:@"PaymentRecieved"
                                          object:nil];
  } 

The notification is being posted from a different view controller after some processes. Below is the code for the same:

ViewController B:

-(void)CardAccepted  
 {
   [[NSNotificationCenter defaultCenter] postNotificationName:@"PaymentRecieved" object:self userInfo:nil];
 } 

The main issue is removeObserver is not working. If I pop back 5 times and then push to same screen 5 times then the observer is added and fired all 5 times.

I have tried everything that was available on Stack Overflow but nothing seems to be working. Below are few of the things that I tried:

Trial 1:

 BOOL isPaymentObserverAdded = [DefaultsValues getBooleanValueFromUserDefaults_ForKey:@"isPaymentObserverAdded"];
 if (!isPaymentObserverAdded) {
 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PaymentRecieved" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(PaymentRecieved:)
                                            name:@"PaymentRecieved"
                                          object:nil];
  } 

 }

 [DefaultsValues setBooleanValueToUserDefaults:YES ForKey:@"isPaymentObserverAdded"];  

Trial 2:

static dispatch_once_t lock;
dispatch_once(&lock, ^{
   [[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(PaymentRecieved:)
                                            name:@"PaymentRecieved"
                                          object:nil];
});  

I know there are so many similar questions on SO for the exact same issue but none of them is solving my problem. Apart from this, I also have couple of other notifications set on this screen.

Can somone please help me to understand what I am doing wrong?

Instead of removing the observer in your viewDidLoad, you should add it to a dealloc method.

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

This will remove all notification observers the object has subscribed to, and is good practice when using observers with pretty much all custom objects.

Try to removeObserver when the user pops from the screen. Like in your back action method.

Like This :

-(void)backAction{

[NSNotificaitonCenter defaultCenter] removeObserver:self name:@"Your Notification name" object:nil]];

}

您可以删除后退按钮上的观察者。确保它可以正常工作。我一周前通过执行此操作解决了此问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM