Search code examples
core-datansmanagedobjectnsundomanagerkey-value-observing

willTurnIntoFault being called more than once, resulting in crash


In a subclass of NSManagedObject my overridden implementation of willTurnIntoFault is being called twice when undoing some code which originally created the object in question. This results in a crash when attempting to double-unregister for KVO on a key path.

The Apple documents say this is the right place to un-register for KVO.

A bit of context - the undo operation involves removing the corresponding view of the model from it's superview. The view retains it's model.

So my question is: what kind of programmer errors can result in willTurnIntoFault being called twice in a subclass of NSManagedObject?

Note: Previously I was overriding dealloc in this class but have since realised this is not recommended for subclasses of NSManagedObject. I've since moved this code into -didTurnIntoFault. I'm not currently overriding any other methods which the Apple docs say you should not override.


Solution

  • Seems the issue was caused by a custom setter method which was setting/unsetting KVO values from within willTurnIntoFault.