具有不同进程的 NSManagedObjectContext [英] NSManagedObjectContext with different processes

查看:71
本文介绍了具有不同进程的 NSManagedObjectContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个进程正在与同一个持久存储进行通信.我将上下文保存在一个进程上,并发布了一个分布式通知.另一个进程看到分布式通知,并再次获取其数据,但仍接收旧数据.我是否需要进行某种刷新"才能让另一个进程从商店中获取正确的数据?

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?

所以,事实证明我正确地刷新了数据.NSManagedObjects 有一个refreshObject:mergeChanges"方法,你可以用它来做到这一点.该问题似乎与时间有关.假设我有两个进程,A 和 B.进程 A 是主进程并保存到数据库.然后进程 B 保存到数据库并向进程 A 发送通知,表明它已经这样做了,然后进程 A 获取新数据.我发现如果进程 A 的保存和进程 B 的保存太接近,即使我刷新,旧数据也会被进程 A 获取.如果我强制两次保存之间有一些时间,那么它会正确运行.

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.

显然,这似乎是某种竞争条件,可能在数据实际保存到数据库之前发送了通知,但是,通知是在托管对象的 didSave 方法中发送的,此时上下文已保存.

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.

推荐答案

您应该检查 merge policy 概念,以在不同上下文之间管理、获取和传达持久存储协调器的正确值.

You should check the merge policy concept, to manage, get and communicate the correct values of a persistent store coordinator between different contexts.

这里 -> http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdChangeManagement.html#//apple_ref/doc/uid/TP30001201-CJBDBHCB

这应该可以解决问题.

希望可以帮到你.

这篇关于具有不同进程的 NSManagedObjectContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆