Core Data / NSTextView 仅在保存后中断

发布于 2024-11-05 14:37:51 字数 314 浏览 6 评论 0原文

我们有一个 NSTextView 和一些关于其内容的数据保存在核心数据托管对象上下文中。当托管对象上下文保留在内存中时,一切都运行良好。然而,当我们保存它时,我们会得到非常奇怪的获取请求行为。

例如,我们运行一个获取请求,询问 textLocation 小于或等于 15 的所有元素。我们返回的数组中的第一个对象的 textLocation 为 16。

我知道我在这里无法得到明确的答案,因为代码相当复杂。但有人知道这个问题有什么味道吗?

我的想法是,我们在保存后没有得到与 NSTextView 同步的正确 MOC?什么可以改变打破这一点?

谢谢。

We have an NSTextView and some data saved about its contents in a core data Managed object context. Everything works great while the managed object context stays in memory. However when we save it, we get very weird fetch request behaviors.

For example, we run a fetch request that asks for all elements with a textLocation less than or equal to 15. The first object in the array we get back has a textLocation of 16.

I know I can't get a definitive answer here, as the code is fairly complex. But does anyone know what this issue smells of?

My thought is that we are somehow not getting the proper MOC synced with the NSTextView after saving? What could change that breaks this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

耳根太软 2024-11-12 14:37:51

例如,我们运行一个获取请求
要求所有带有 a 的元素
textLocation 小于或等于 15。
我们得到的数组中的第一个对象
背面的 textLocation 为 16。

实际上,获得该值的唯一方法是(按照可能性的相反顺序):

  1. 搞乱属性的定义,使您认为您正在保存一种类型的数字信息,但实际上您正在保存其他。
  2. 您已对谓词进行了修改,以便它实际上查找 16 或更大的值。 (您可以针对字典数组测试谓词,这些字典的键与核心数据实体具有相同的名称。)
  3. 这是为了在 UI 中显示或记录的目的而在数字和字符串之间进行转换时出现的错误。

我自己会从 (3) 开始,因为它似乎更常见,并且在您确认没有显示问题之前,您无法诊断其他问题。

For example, we run a fetch request
that asks for all elements with a
textLocation less than or equal to 15.
The first object in the array we get
back has a textLocation of 16.

Really, the only way to get that is to (in reverse order of likelihood):

  1. Mess up ethe definition of the attribute such that you think your are saving one type of numerical info but that you are actually saving another.
  2. You've mangled the predicate so that it actually looks for values of 16 or greater. (You can test predicates against an array of dictionaries whose keys have the same names as you Core Data entities.)
  3. It's an error in the conversion between a number and a string for purposes of displaying in the UI or logging.

I would start with (3) myself because it seems more common and until you confirm you don't have a display problem, you can't diagnose the other problems.

夜无邪 2024-11-12 14:37:51

我终于弄清楚发生了什么事。我使用 setPrimitiveValue 设置 textLocation...只是因为我不想触发通知。事实证明这是一个非常糟糕的主意,因为核心数据不知道值已经改变。它仍然认为该值是 15 而不是 16。

让这成为一个教训:永远不要绕过 KVO,除非您在托管对象内部并且知道自己在做什么!

I finally managed to work out what was going on. I was setting textLocation using the setPrimitiveValue... just because I didn't want notifications to fire off. Turns out that's a really bad idea, because Core Data didn't know the value had changed. It still thought the value was 15 instead of 16.

Let this be a lesson: never bypass KVO unless you're INSIDE the managed object and you know what you're doing!

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