我有一个NSManagedObject的子类,大多数属性是动态的(由托管对象创建),但我有一些辅助属性,它们被创建为@synthesize.
释放这些物体的正确方法是什么?
- (void)didTurnIntoFault { [self.randomVar release]; [super didTurnIntoFault]; }
要么
- (void)dealloc { [self.randomVar release]; [super dealloc]; }
解决方法
覆盖didTurnIntoFault并在那里释放它们.管理对象在出现故障时不一定要解除分配.
You are discouraged from overriding
dealloc
orfinalize
becausedidTurnIntoFault
is usually a better time to clear values—a managed object may not be reclaimed for some time after it has been turned into a fault. Core Data does not guarantee that eitherdealloc
orfinalize
will be called in all scenarios (such as when the application quits); you should therefore not in these methods include required side effects (like saving or changes to the file system,user preferences,and so on).