奇怪的 CoreData 问题和更奇怪的解决方案。知道为什么吗?
我是一个非常舒服的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 aroundwillChangeValueForKey:
anddidChangeValueForKey:
calls.