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)?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With