强制托管对象变脏
有没有办法强制托管对象进入脏状态?我有一个与父托管对象有关系的托管对象。如果我更改子托管对象的属性,我很好奇是否有办法将父托管对象置于脏状态。
Is there a way to force a managed object into the dirty state? I have a managed object that has a relationship to a parent managed object. If I change a property on the child managed object, I was curious if there is a way to put the parent managed object into a dirty state.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSPersistentDocument
,这会很有用。UIDocument
。如果这些不可能,那么您可以让父级观察其子级的更改,或者让子级在其父级上设置更改标志。无论哪种情况,您都可以修改某些“最后更改的”属性,从而导致自己变脏。
您还可以创建一个类似
-hasChangedChildren
的方法,该方法将遍历子树并在有脏数据时返回YES
。这样做的优点是不实际修改对象,因此不会影响任何核心数据优化。文档并不禁止修改hasChanges
来实现这种行为,但我个人会小心这样做。但如果可能的话,您应该使用文档类,因为这就是它们的用途。
您可能还对 核心数据问题——关系、UUID 和脏状态。
NSPersistentDocument
for most cases where this would be useful.UIDocument
.If these aren't possible, then you can either have the parent observe changes in its children, or have children set a changed flag on their parent. In either case, you can modify some "last changed" property to cause yourself to become dirty.
You can also create a method like
-hasChangedChildren
that would walk the children tree and returnYES
if any are dirty. This has the advantage of not actually modifying the object, so you don't impact any Core Data optimizations. The docs do not forbid modifyinghasChanges
to behave this way, but I would personally be careful doing so.But if at all possible, you should use the document classes, since this is what they're for.
You may also be interested in Core Data Questions--Relationships, UUIDs, and Dirty States.