Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KVO: Can I remove all observers from concrete object

I'am using key-value observing. I have object_1 (NSManagedObject) and few other objects-observers. When I remove object_1 from managed object context my program crashes.

CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.

Can I put something to dealloc method (or somewhere else) to remove all observers of object_1? Or the only appropriate decision is to send notification right when I am about to remove object_1 from managed object context and listen to this notification by other objects (to remove themselves from observers of object_1)?

like image 474
Lloyd18 Avatar asked Oct 20 '11 07:10

Lloyd18


1 Answers

You need to ensure your observers are removed before your object is deallocated. This is a downside of Key Value Observer.

As you suggest, one way to do this is through removal triggered from within your dealloc method. Obviously you can't know who has observers on your object but will still need to notify them.

I'd describe what you need to do but perhaps just check out bj-homer's answer in question When should I remove observers? Error about deallocating objects before removing observers.

like image 101
wmorrison365 Avatar answered Nov 17 '22 00:11

wmorrison365