7

I have implemented NSNotificationCenter in my application. I send notification when ever image decoding is done. first time the image decoding will be done 8 times.So the notification suppose to send 8 times.But it is calling 64 times(8*8).

Here is my code how i have implemented--> // Initialisation

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
 if (self) {

    [[NSNotificationCenter defaultCenter] addObserver:self
                                                      selector:@selector(getHRImage:)
                                                             name:@"DecodeComplete" object:nil];}   

//Calling Method

 -(void)getHRImage:(NSNotification *) notification
{

if ([[notification name] isEqualToString:@"DecodeComplete"])
    NSLog (@"Successfully received the DecodeComplete notification! ");
}`

// Deallocation

- (void) dealloc
{
      [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DecodeComplete" object:self];
    //[super dealloc];
}

//Post Notification

[[NSNotificationCenter defaultCenter] postNotificationName:@"DecodeComplete" object:self];

Some one can suggest me where I have done wrong.

Thanks in advance.

//Calling Method is like this (calling 8 times)

-(void)decode
{
   NSLog(@"---------- Decoding is Complete ---------");
     [[NSNotificationCenter defaultCenter]  postNotificationName:@"MdjDecodeComplete" object:self];

}
Kiran Kumar
  • 677
  • 3
  • 17
  • 2
    You haven't shown enough context around your call to `postNotificationName` to provide an answer, but you must be calling this 64 times - so I suspect there is some problem with your loop structure. I suggest you either post more code or place a breakpoint on `postNotificationWithName` and see where it is being called – Paulw11 Apr 20 '15 at 09:29
  • have you checked that your `dealloc` methods is called or not? – Jatin Patel - JP Apr 20 '15 at 09:34
  • it seems that you are adding observe multiple times and your previous added observe is not removed so you are getting getHRImage method multiple times. – Jatin Patel - JP Apr 20 '15 at 09:36
  • check some points in project 1. only one object exist in memory when you fire notification. 2.check how many times initWithFrame called before fire notification. 3. @None already mention and last one check your code when you fire a notification. – SGDev Apr 20 '15 at 09:38
  • 1
    @None the dealloc method is calling when ever i go back. i rechecked there is no multiple observers. – Kiran Kumar Apr 20 '15 at 09:42
  • Please print the pointer (format string: `%p`) to the notification object in `-getHRImage:`, too. You can see, whether the method is executed 8 times per notification (= 8 registrations) or not. – Amin Negm-Awad Apr 20 '15 at 09:46
  • @KiranKumar, kindly check that number of times `AddObserve` called is equal to number of times `removedObserve` called? – Jatin Patel - JP Apr 20 '15 at 09:47
  • @Sumith Garg 2. initWithFrame 8 times calling – Kiran Kumar Apr 20 '15 at 09:50
  • @ None when ever i pressed the back button remove observer is calling 6 times. – Kiran Kumar Apr 20 '15 at 09:54

2 Answers2

5

Solution: I rechecked my code, the initWithFrame:(CGRect)frame is calling 8 times and adding NSNotification observer 8 times.

So i have changed my code like this, --->> Initialisation.

static bool note=YES;
- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
  if(note)
 [[NSNotificationCenter defaultCenter] addObserver:self

                                                  selector:@selector(getHRImage:)
                                                         name:@"DecodeComplete" object:nil]; note=NO;}   

--->> Deallocation

- (void) dealloc
  {
    note=true;

  [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DecodeComplete" object:nil];
//[super dealloc];
}

Now addObserver method is calling only one time so my problem is resolved. Thank you all for your valuable guidance.

Kiran Kumar
  • 677
  • 3
  • 17
-2

- (void) dealloc will not be called in ARC environment. Instread, you can remove your observer before add it like this:

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DecodeComplete" object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getHRImage:) name:@"DecodeComplete" object:nil];     
  }
}
Lazy_Clutch
  • 130
  • 3
  • 14
  • 5
    dealloc will be called in ARC, so any customization in dealloc is still legal. You must make some mistake here. – user3349433 Apr 20 '15 at 09:32
  • This does not work for two reasons: 1. `-dealloc` is executed as mentioned by @user3349433, 2. it would remove the *new* view instance, not an existing one. – Amin Negm-Awad Apr 20 '15 at 09:47