forum

Home / DeveloperSection / Forums / Which is the better way to remove self from NSNotificationCenter

Which is the better way to remove self from NSNotificationCenter

Tarun Kumar 1947 14-Jul-2015
Which is the better way to remove self from NSNotificationCenter? Just remove self or remove self from the specific notification name?

I just want to know: Which is the better way to remove self from NSNotificationCenter in dealloc method? Or is anyone have met different behaviors between the two way?

PS. I just catch a weird thing that when I remove self from notification center by the first way but the object which is dealloced still can recieve the notification, and this call a crash exception of course.

Just remove self by follow code:

 [[NSNotificationCenter  defaultCenter] removeObserver:self];

or remove self from the specific notification name like this:

 [[NSNotificationCenter  defaultCenter] removeObserver:self name:NotificationName object:someObj];

PS. I just catch a weird thing that when I remove self from notification center by the first way but the object which is dealloced still can recieve the notification, and this call a crash exception of course.


Updated on 14-Jul-2015

Can you answer this question?


Answer

2 Answers

Liked By