Users Pricing

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 2332 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.


Tarun Kumar

Other


2 Answers