将两个SQLite存储组合成一个 [英] Combining Two SQLite Stores Into One

查看:96
本文介绍了将两个SQLite存储组合成一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的应用程序文档目录中有两个SQLite文件。我如何将两者结合在一起并将它们保存为包含两者所有信息的单个文件?我是否真的必须创建两个持久存储协调器并手动协调两者之间的过程,还是有更有效的方法呢?

Say I have two SQLite files in my application documents directory. How would I go about combining the two together and saving them as a single file that contained all the information from both? Do I literally have to create two persistent store coordinators and manually coordinate the process between the two, or is there some more efficient way of doing it?

-Ash

推荐答案

你的问题不是你需要问的问题。 Core Data中的SQLite存储仅仅是Core Data对象图的后备存储。事实上,它是一个SQLite数据库对Core Data来说并不重要。所以你的问题是,如何将两个Core Data对象图合并为一个?

Your question isn't quite what you need to ask. A SQLite store in Core Data is merely the backing store for a Core Data object graph. The fact that it's a SQLite database is immaterial to Core Data. So your question is really, "how do I merge two Core Data object graphs into one?"

这取决于你想对数据做什么。 Core Data中的一个规则是您不能在不同持久性存储中的两个对象之间建立关系。因此,如果您不需要存储A中的对象与存储B中的对象之间的关系,那么它非常简单。只需将两个持久存储添加到 NSPersistentStoreCoordinator ,两个存储中的所有对象都可以从与该持久存储协调器关联的任何托管对象上下文中获得。

It kind of depends on what you want to do with the data. One of the rules in Core Data is that you can't have relationships between two objects in different persistent stores. So, if you don't have a need for relationships between objects in Store A and objects in Store B, it's really quite simple. Just add both persistent stores to your NSPersistentStoreCoordinator, and all the objects from both stores will be available from any managed object context associated with that persistent store coordinator.

如果您确实需要在两个商店中的对象之间建立关系,或者您真的只想拥有一个商店,那么您最好的选择是创建第二个 NSPersistentStoreCoordinator 和第三个不同的持久存储来保存合并的对象图。您需要编写代码以在附加到第二个 NSPersistentStoreCoordinator 的托管对象上下文中创建对象的副本。您还需要在原始对象的副本之间设置相同的关系,但是如何执行此操作取决于您的数据模型。如果您想在问题中添加更多细节,也许我可以让您更好地了解所涉及的内容。

If you do need to have relationships between the objects in both stores, or you really just want to have a single store, your best bet would be to create a second NSPersistentStoreCoordinator and a third, distinct persistent store to hold the merged object graph. You will need to write code to create copies of the objects in a managed object context attached to this second NSPersistentStoreCoordinator. You'll need to set up the same relationships among the copies that the original objects had, too, but how you go about doing this depends on your data model. If you want to add some more detail to your question, maybe I can give you a better idea of what would be involved.

这篇关于将两个SQLite存储组合成一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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