3

I have two class which uses NSNotification to communicate with each other.

Currently, i have an issue with notification being fired twice, i've double/triple/even more checked that observer is not added more then 1 time, notification not being posted twice, did global search on my project for same notification.

My code is like below


Added Notification Observer

[[NSNotificationCenter defaultCenter] removeObserver:self name:notification_deleteMediaFromGallery object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationReceiver:) name:notification_deleteMediaFromGallery object:nil];

Notification Receiver

- (void)notificationReceiver:(NSNotification*)notification {
    if ([notification.name isEqualToString:notification_deleteMediaFromGallery]) {
        if ([[notification.userInfo objectForKey:@"kind"] integerValue]==GalleryKindPhoto) {
            //My statements
        }
        else if ([[notification.userInfo objectForKey:@"kind"] integerValue]==GalleryKindVideo) {
            //My statements
        }
    }
}

Post Notification

dispatch_async(dispatch_get_main_queue(), ^{
    [_browser reloadData];
    [[NSNotificationCenter defaultCenter] postNotificationName:notification_deleteMediaFromGallery object:nil userInfo:@{@"index":@(_browser.currentIndex), @"kind":@(self.kind), @"function":[NSString stringWithFormat:@"%s",__PRETTY_FUNCTION__]}];
});

I have also tried this solution by EmptyStack but not get it to work.

I'll be very thankful to you if you could help me solve this issue.

Thanks.

Edit

NOTE

I've added observer in my viewdidload, and cant add/remove observer from viewwillappera/viewwillappear or viewdidappear/viewdiddisappear because the next viewcontroller which will be pushed on current viewcontroller will post notifications

Community
  • 1
  • 1
Pratik Jamariya
  • 810
  • 1
  • 10
  • 35
  • May be u have tried this , but could you tell us what happens after you add some print statement at both places ; one where u post and one where u register – humblePilgrim Mar 03 '17 at 06:52
  • @humblePilgrim yeah i did try that, it logs once where i post my notification but in receiver it logs twice with same address number of notification – Pratik Jamariya Mar 03 '17 at 08:01
  • i'll post log of notification in about an hour – Pratik Jamariya Mar 03 '17 at 08:09
  • 1
    Set a breakpoint where you post your notification. If you only subscribed once (I hope you verified via NSLog or breakpoint) then you must be _posting_ it twice. – DarkDust Mar 03 '17 at 08:11
  • I don't know what causing the problem, I'll have to dig the problem out, I've set breakpoints, nslogs, etc but couldn't find out. May be i think it's because I've done something wrong. At the end of the day after digging alot it still called twice. I'm thinking of trying out with delegate cuz i don't have much time for solving this issue. May be I'll take a backup to find out later what's causing this – Pratik Jamariya Mar 03 '17 at 15:21

2 Answers2

0

I think you need to write dealloc method in your view controller. And remove All Notification observer in dealloc method,

- (void)dealloc
{
    // Deregister observer
    [[NSNotificationCenter defaultCenter] removeObserver:self name:notification_deleteMediaFromGallery object:nil];
}
jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29
  • Well, thanks for the reply but i don't think dealloc method will get called as these classes are my viewcontrollers, popping back will call `- (void)viewWillDisappear:(BOOL)animated` and `- (void)viewDidDisappear:(BOOL)animated`. BTW i have another hint in my mind, lemme try that out – Pratik Jamariya Mar 03 '17 at 06:50
  • As per my knowladge you need to add addObserver in ViewDidLoad() methos and remove that in dealloc method. if you want to remove in viewDidDisappear then you need to write code for addObserver in viewWillappear method . – jignesh Vadadoriya Mar 03 '17 at 06:56
  • No buddy, I've added the observer in my `viewDidLoad`, and i cant remove observer in `viewdiddisappear` cause the next viewcontroller will post the notification. and yeah the dealloc method will never get called because dealloc gets called when the object totally removed from memory, for more clearance please [check this answer out](http://stackoverflow.com/a/30332523/3308174) – Pratik Jamariya Mar 03 '17 at 07:02
-2
Hi please make sure your method is not calling two time from where you are firing notification.

& please add your notification observer in viewWillDisappear method. 
Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36