NSManagedObjectContext 具有不同的进程
我有两个进程正在与同一个持久存储进行通信。我将上下文保存在一个进程上,然后发布一个分布式通知。另一个进程看到分布式通知,并再次获取其数据,但仍然收到旧数据。我是否需要进行某种“刷新”才能让其他进程从商店获取正确的数据?
编辑:所以,事实证明我正确地刷新了数据。 NSManagedObjects 有一个“refreshObject:mergeChanges”方法,您可以使用它来执行此操作。该问题似乎与时间相关。假设我有两个进程,A 和 B。进程 A 是主进程,它会保存到数据库。然后,进程 B 向数据库进行保存,并向进程 A 发送已完成保存的通知,然后进程 A 获取新数据。我发现如果进程 A 的保存和进程 B 的保存距离太近,即使我刷新,进程 A 也会获取旧数据。如果我强制在两次保存之间留出一些时间,那么它就会正确地工作。
显然,这看起来像是某种竞争条件,其中可能在数据实际保存到数据库之前发送通知,但是,通知是在托管对象的 didSave 方法中发送的,此时上下文已经已保存。
I have two processes that are talking to the same persistent store. I save the context on one process, and I post a distributed notification. The other process sees the distributed notification, and fetches its data again, but still receives the old data. Is there some kind of "flushing" I need to do to get the other process to get the correct data from the store?
EDIT: So, it turns out that I was flushing the data correctly. NSManagedObjects have a "refreshObject:mergeChanges" method that you use to do this. The issue appears to be timing related. Let's say I have two processes, A and B. Process A is the main process and does a save to the database. Then Process B does a save to the database and sends a notification to Process A that it has done so, and Process A fetches the new data. I've found that if Process A's save and Process B's save are too close together, the old data is fetched by Process A even if I refresh. If I force there to be some time between the two saves, then it works out correctly.
Obviously this seems like some kind of race condition, where perhaps the notification is getting sent before the data is actually getting saved to the database, however, the notification gets sent in the didSave method of the managed object, at which point the context has already saved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该检查合并策略概念,以在不同上下文之间管理、获取和传达持久存储协调器的正确值。
这里-> http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdChangeManagement.html#//apple_ref/doc/uid/TP30001201-CJBDBHCB
这应该可以解决问题。
希望这能有所帮助。
You should check the
merge policy
concept, to manage, get and communicate the correct values of a persistent store coordinator between different contexts.Here -> http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdChangeManagement.html#//apple_ref/doc/uid/TP30001201-CJBDBHCB
That should resolve the problem.
Hope this can help.