核心数据:合并多个托管对象上下文并删除对象
我有一个 iPhone 项目,其中主应用程序线程内的托管对象上下文中有一系列托管对象。在一个单独的线程上,我将新对象从网络服务器导入到第二个托管对象上下文中。
如果导入顺利且没有错误,我会保存导入上下文。这会触发 contextDidSave,我从中调用 mergeChangesFromContextDidSaveNotification
。两个上下文合并正常。到目前为止,一切都很好。
我想要的功能是删除原始 MOC 中但不在导入的 MOC 中的任何对象(想法是用户正在刷新数据,因此应删除旧数据),但合并似乎只是合并 2 个 MOC。
请您告知是否有一种方法可以管理 2 个 MOC 的合并,以便将不在一个 MOC 中的内容从另一个 MOC 中删除?
I have an iPhone project where I have a series of Managed Objects in a Managed Object Context within the main application thread. On a separate thread, I import new objects from a webserver into a second Managed Object Context.
If the import goes ok without errors, I save the import context. This triggers contextDidSave from which I call mergeChangesFromContextDidSaveNotification
. The two contexts merge ok. So far, so good.
My desired functionality is to have any objects that are in the original MOC but not in the imported MOC to be deleted (the idea is that the user is refreshing the data, and so old data should be deleted), but the merge seems to just combine the 2 MOCs.
Please can you advise if there is a way of managing the merge of the 2 MOCs so that those not in one are deleted in the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您创建新的 moc 时,它可以访问所有旧对象,因此您可以在导入过程中处理后台线程上的删除。
When you create the new moc it has access to all of the old objects so you can handle the deletion on the background thread as part of the import.
你所描述的并不只是进口。决定“更新”哪些对象的逻辑是特定于应用程序的。您可以在导入线程上通过获取这些对象并更新它们来处理该问题,而不是创建新实例,或者您可以在会面后完成该工作。在这两个选项中,我更喜欢第一个,因为它更符合核心数据的方式。
What you describe is not really just importing. The logic of deciding what objects are being "updated" is application specific. You can handle that on the import thread by fetching those objects and updating them rather than creating new instances or you can do the work after meting. Of the two options, I would prefer the first one as it's more inline with the Core Data way of things.
在这种情况下,我所做的是一种方法“prepareForDelet”,它将属性“删除”标记为“是”。
然后,当我导入对象时,如果已经存在,我将标记为“删除否”。
当我的线程结束时,我可以删除每个删除为 YES 的对象
What I do in such situation, is a method "prepareForDelet" that marks a property "delete" to YES.
Then when I import my objects, if one exists already, I mark as delete NO.
When my thread is over, I can delete every objects whose delete is YES