Search code examples
objective-ciosnsnotifications

Objective C: Any issues in having 2 NSNotifications set up in a single class?


I have a class with 2 NSNotifications implemented

    //Set up notifications
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(getData)
                                                 name:@"Answer Submitted"
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(reloadTable)
                                                 name:@"Comment Submitted"
                                               object:nil];

I would just like to check if it is ok to set 2 observers in a single class? Also when I remove observer, I am only removing one observer in dealloc method. Is that an issue?


Solution

  • It is perfectly fine to have more than one observer in a single class. You should always unregister the observer once you're done with it.

    More details on the Observer pattern in Objective-C can be found here.