I have an object ivar foo inside a NSManagedObject subclass bar that I need to be there at all times, as long as the object exists.
To make sure foo is created properly, I’ve subclassed awakeFromInsert to create foo when bar is created. I’ve done the same in awakeFromFetch, to make sure foo is there when bar is fetched from the store.
To counteract that, I release foo inside willTurnIntoFault and in prepareForDeletion.
However, it turns out that when I do delete bar, both prepareForDeletion and then willTurnIntoFault are called, releasing foo twice.
I realize I can probably just not release it in prepareForDeletion then, but I’d like to know what the best practice is here, so I understand when something gets turned into a fault, etc. For a normal object, I’d just create foo in init and destroy it in dealloc.
Thanks!
Instead of just releasing the ivar, release it and set it to
nil. Releasingnilhas no effect, so you’ll be OK if it happens twice.Better yet, make
fooa property withretainsemantics and always set it via-setFoo:.