Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my NSNotification its observer called multiple times?
    primarykey
    data
    text
    <p>Within an App I make use of several viewcontrollers. On one viewcontroller an observer is initialized as follows:</p> <pre><code>[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod:) name:@"MyNotification" object:nil]; </code></pre> <p>Even when removing the <code>NSNotification</code> before initializing the number of executions of <code>myMethod:</code> is being summed up by the amount of repeated views on the respective viewcontroller.</p> <p>Why does this happen and how can I avoid myMethod: being called more then once.</p> <p>Note: I made sure by using breakpoints that I did not made mistakes on calling postNotification multiple times.</p> <p>Edit: This is how my postNotification looks like</p> <pre><code>NSArray * objects = [NSArray arrayWithObjects:[NSNumber numberWithInt:number],someText, nil]; NSArray * keys = [NSArray arrayWithObjects:@"Number",@"Text", nil]; NSDictionary * userInfo = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:self userInfo:userInfo]; </code></pre> <p>edit: even after moving my subscribing to viewwillappear: I get the same result. myMethod: is called multiple times. (number of times i reload the viewcontroller).</p> <pre><code>-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod:) name:@"MyNotification" object:nil]; } </code></pre> <p>edit: something seems wrong with my lifecycle. ViewDidUnload and dealloc are not getting called, however viewdiddisappear is getting called.</p> <p>The way I push my Viewcontroller to the stack is as follows where parent is a tableview subclass (on clicking the row this viewcontroller is initiated:</p> <pre><code>detailScreen * screen = [[detailScreen alloc] initWithContentID:ID andFullContentArray:fullContentIndex andParent:parent]; [self.navigationController pushViewController:screen animated:YES]; </code></pre> <p>Solution:</p> <p>Moving removal of nsnotification to viewdiddisappear did the trick. Thanks for guidance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload