1

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?

Milan Gupta
  • 1,181
  • 8
  • 21
  • viewDidLoad is not called when you popBAck to the same class. – Teja Nandamuri Oct 20 '17 at 14:51
  • observer is added only once. Do you mean notification is being called 5 times ? Where are you calling the CardAccepted method ? – Teja Nandamuri Oct 20 '17 at 14:53
  • @TejaNandamuri yes viewDidLoad is not called during pop back but after I pop back, I have to push again to this view to continue process. CardAccepted method is in another view controller. – Milan Gupta Oct 20 '17 at 16:30
  • it is fine that way , what do you mean by " fired all 5 times." ? Is it the notification ? – Teja Nandamuri Oct 20 '17 at 17:02
  • I am not sure, but `PaymentRecieved ` method is called all 5 times, sometime 3 times, depends how many times I have pushed to the View COntroller. – Milan Gupta Oct 20 '17 at 17:14
  • I'm not sure but it *seems* that you want to do something through pushing one ViewController, but then let it only happen once. meaning if the you push the viewController a 2nd time, you don't want it to happen again. Is that right? – mfaani Oct 23 '17 at 11:23
  • For this issue I always use this way https://stackoverflow.com/a/5660130/5251783 , please check it . – Hosny Oct 23 '17 at 13:57

3 Answers3

0

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.

royalmurder
  • 148
  • 1
  • 10
0

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]];

}

0

You can remove observer on back button.sure it will work.i solved this issue just a week ago by doing this

  • while register notification do this. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"" object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"" object:dict]; –  Oct 24 '17 at 10:04