具有非托管属性的托管对象会出错吗?
核心数据文档指出,如果需要,对象可能会错误地节省内存。如果您的对象具有非托管属性的属性,会发生什么情况?
例如,假设您有一个 Department 类,它是 NSManagedObject 的子类。它有一个位置 iVar + 访问器。 location 属性不是 Department 的属性;它不受管理,也从未持续存在。
如果您有一个 Department 对象数组,或者一个与 Department 具有一对一关系的 Employee 对象,那么 Department 是否可能会出错?如果你设置了Department.location,你能保证这个location永远都在吗?还是有可能Department会出错,然后你就丢失了location中存储的值?
The Core Data documentation says that objects may fault to save memory if needed. What happens if you have an object that has a property that is not a managed property?
For example, say you have a Department class, that is a subclass of NSManagedObject. It has a location iVar + accessors. The location property is not an attribute of Department; it is not managed and never persisted.
If you have an array of Department objects, or an Employee object with a to-one relationship to Department, is it possible for the Department to ever fault? If you set the Department.location, can you be sure that the location will always be there, or is it possible that the Department will fault, and then you will have lost the value stored in location?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该部门仍然有可能犯错,它只会犯错您在模型中描述的属性。通常,您所描述的情况由“瞬态”属性涵盖,这些属性不存储在 CoreData 中,但对象模型知道它们。
当您实现瞬态属性时,您提供了为该属性提供值所需的存储(或计算)。
在您的情况下,假设您的“位置”值将来不会存在是完全合理的,因为只有当实际的托管对象保留在内存中时它才会存在。换句话说,任何导致托管对象被释放的操作(例如上下文重置、保存或来自保存通知的更新)都可能导致该值丢失(因为它所绑定的托管对象变成了故障或无效) )。
It is still possible for the Department to fault, it will just fault properties you have described in your model. Typically the situation you are describing is covered by "transient" properties, which are properties that are not stored in CoreData, however the object model is aware of them.
When you implement a transient property you provide the storage (or computation) required for providing the value for that property.
In your case it is perfectly legitimate to assume that your "location" value will not be there in the future, as it will only survive so long as the actual managed object remains in memory. In other words, any action which causes the managed object to be released such as a context reset, save or update from a save notification may cause the value to be lost (as the managed object it is tied to is turned into a fault or invalidated).
我不相信非托管数据会留在那里。即使现在这样做,这种行为将来也可能会改变。更重要的是,从架构上讲,我根本不建议将非托管数据保留在托管对象中。你最好这样做:
I would not trust unmanaged data to remain there. Even if it does now, the behavior may change in the future. More so, architecturally I would not recommend keeping unmanaged data in the managed object at all. You're better off: