奇怪的 CoreData 问题和更奇怪的解决方案。知道为什么吗?

发布于 2024-09-17 19:19:01 字数 696 浏览 4 评论 0原文

我是一个非常舒服的 CoreData 用户。我遇到了一个绝对让我丧命的问题,我弄清楚出了什么问题,但不知道为什么它会导致我所看到的问题。谁能解释一下吗?

好的,我们有一个主 MOC,它执行大部分“只读”数据工作并处理简单的写入操作。当我们要做一个大的写入/导入/等操作时,我们在一个新线程上分配一个后台 MOC,完成所有繁重的工作,然后保存它。保存会导致 NSManagedObjectContextWillSaveNotification 触发,然后我们 mergeChangesFromContextDidSaveNotification: 并且主 MOC 上的所有内容都会反映更改。相当标准。

因此,几天前,NSManagedObject 子类之一发生了变化。更改是 setValue:forKey: 中的调用。当该方法中没有进行特殊处理时,我们最初调用的是 [super setValue: forKey:]。它被更改为 [self setPrimitateValue: forKey:] 并且...

保存后,将调用 mergeChangesFromContextDidSaveNotification: ,但主 MOC 不会反映更改。所以这有点奇怪。但是关闭应用程序然后再次打开它,更改就会神奇地出现。什么!?!

我知道持久存储保存更改存在一些问题,但其他 MOC 则不然。然后,当新的 MOC 创建新的发布时,一切都会再次陷入困境。为什么?

关于这个话题的任何启发都会对很多人有所帮助。

I'm a pretty comfortable CoreData user. I had a problem that was absolutely killing me, figured out what was going wrong and have NO CLUE why it was causing the problems I was seeing. Can anyone explain this?

Okay, we have a main MOC that does most the 'read-only' data work and handles simple write operations. When we're about to do a big write/import/etc, we allocate a background MOC on a new thread, do all the heavy work, then save it. Saving causes our NSManagedObjectContextWillSaveNotification to fire and then we mergeChangesFromContextDidSaveNotification: and everything on the main MOC reflects the changes. Pretty standard.

So a few days ago a change was made in one of the NSManagedObject subclasses. The change was a call in setValue:forKey:. When no special handling was done in that method, we were originally calling [super setValue: forKey:]. It was changed to [self setPrimitateValue: forKey:] and...

After a save the mergeChangesFromContextDidSaveNotification: would get called, but the main MOC wouldn't reflect the changes. So that's a bit weird. But close the application and then opening it again, the changes would magically BE THERE. What!?!

I know it's some problem with the persistant store saving the changes but not the other MOC. Then when the new MOC gets created new launch, everything is in sink again. Why?

Any light on this topic would help a bunch.

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

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

发布评论

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

评论(1

木緿 2024-09-24 19:19:01

setPrimitiveValue:forKey: 不会导致所有 kvo 通知触发。我认为这就是你的问题的原因。您可能需要将其包装在 willChangeValueForKey:didChangeValueForKey: 调用周围。

setPrimitiveValue:forKey: doesn't cause all the kvo notifications to fire. I think this is the cause of your problem. You probably need to wrap it around willChangeValueForKey: and didChangeValueForKey: calls.

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