对于Xcode生成的Core Data托管对象,我是否需要添加dealloc方法来释放变量?
对于Xcode生成的Core Data托管对象,我是否需要添加dealloc方法来释放变量?
因此,当我的 iPhone 应用程序有一个核心数据模型,并且让 XCode 生成托管对象类时,我注意到没有 dealloc 方法。我是否需要自己在 dealloc 方法中手动“释放”变量/属性?
我看到代码生成的托管对象类具有:
- 该属性在实现文件的头文件中被标记为“retain”,
- 它是用“@dynamic”(即不是@sythesise)设置的,
谢谢
for Xcode produced Core Data managed objects, do I need to add a dealloc method to release variables?
So when I have a core data model for my iPhone app, and I let XCode generate the managed object classes, I note there is no dealloc method. Do I need to "release" in a dealloc method myself manually to the variables/properties?
I see the code-generated managed object classes have:
- the property is marked "retain" in the header file
- in the implementation file it is set up with "@dynamic" (i.e. not @sythesise)
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要(也不应该)修改这些生成的文件。 @dynamic 意味着属性实现将在运行时提供。 动态属性
You don't need to (and shouldn't) modify these generated files. The @dynamic means that the property implementations will be provided at runtime. Dynamic Properties
这有两个部分。对于您在模型中定义的属性,不要在 dealloc 方法中释放它们。 Core Data 正在为您管理这些内容。但是,如果您将其他 ivars 添加到生成的类中,假设您有一个 imageData 属性,然后从中创建一个 UIImage 并将其保存在 NSManagedObject 子类中,那么您需要添加 dealloc 方法并释放它,就像在任何其他对象。
我强烈建议您开始使用 mogenerator。这很好,因为它会生成一个机器文件和一个用户文件,以将自动处理的内容与自定义代码分开。
There's two parts to this. For the attributes that you define in you're model, don't release them in a dealloc method. Core Data is managing those for you. But if you add other ivars to the class that gets generated, say you have an imageData attribute and then make a UIImage out of it that you hold in your NSManagedObject subclass, then you need to add the dealloc method and release it like you would in any other object.
I'd highly recommend you start using mogenerator. It's nice since it generates a machine file and a user file to keep the stuff that's handled automatically for you separate from your custom code.
虽然您不需要释放 Core Data 托管属性,但您应该清理自定义 Core Data 子类创建的所有属性或实例变量。但是,您不能依赖在 Core Data 子类上调用 dealloc。使用 willTurnIntoFault 执行任何必要的清理。
While you do not need to release Core Data managed properties, you should clean up any properties or instance variables your custom Core Data subclass creates. However, you cannot rely on dealloc to be called on Core Data subclasses. Use willTurnIntoFault to perform any cleanup necessary.