Zhen
Zhen

Reputation: 12431

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?

Upvotes: 2

Views: 80

Answers (1)

csano
csano

Reputation: 13696

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.

Upvotes: 4

Related Questions