开发者

NSManagedObject init/dealloc equivalent

开发者 https://www.devze.com 2023-01-23 23:13 出处:网络
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.

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,开发者_StackOverflow中文版 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. Releasing nil has no effect, so you'll be OK if it happens twice.

Better yet, make foo a property with retain semantics and always set it via -setFoo:.

0

精彩评论

暂无评论...
验证码 换一张
取 消