Combining Two SQLite Stores Into One
Say I have开发者_StackOverflow 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
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?"
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.
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.
Not sure what a store coordinator is, but can't you just dump the data from the first, and load it in the second?
精彩评论