强制托管对象变脏

发布于 2024-12-13 03:43:02 字数 78 浏览 2 评论 0原文

有没有办法强制托管对象进入脏状态?我有一个与父托管对象有关系的托管对象。如果我更改子托管对象的属性,我很好奇是否有办法将父托管对象置于脏状态。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放飞的风筝 2024-12-20 03:43:03
  • 在 Mac 上,您应该在大多数情况下使用 NSPersistentDocument,这会很有用。
  • 在 iOS 5+ 上,您应该使用 UIDocument

如果这些不可能,那么您可以让父级观察其子级的更改,或者让子级在其父级上设置更改标志。无论哪种情况,您都可以修改某些“最后更改的”属性,从而导致自己变脏。

您还可以创建一个类似 -hasChangedChildren 的方法,该方法将遍历子树并在有脏数据时返回 YES。这样做的优点是不实际修改对象,因此不会影响任何核心数据优化。文档并不禁止修改 hasChanges 来实现这种行为,但我个人会小心这样做。

但如果可能的话,您应该使用文档类,因为这就是它们的用途。

您可能还对 核心数据问题——关系、UUID 和脏状态

  • On Mac, you should use NSPersistentDocument for most cases where this would be useful.
  • On iOS 5+, you should use 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 return YES 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 modifying hasChanges 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文