views:

36

answers:

2

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

+1  A: 

Not sure what a store coordinator is, but can't you just dump the data from the first, and load it in the second?

Evert
Dunno. Persistent Store Coordinators are new to me as well. I would have preferred to use the old Datastream method of saving and loading, but that's been deprecated for a while now.
Ash
Persistent store coordinators are part of Core Data. You can't just dump data from one persistent store into another and expect it to work. You should treat the SQLite stores as black boxes because you can easily break things by messing about in them by yourself.
Alex
+1  A: 

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.

Alex
Thanks. At the moment I'm not sure which approach would be necessary. I prefer the former since it's easier, but I don't know if there would be any negative side-effects to having several files floating around when I only need one.
Ash
Yes, you'll need a second persistent store coordinator to fetch objects from the one you're copying from, and then you'll need to create copies of those objects in your "main" object graph. Since objects are associated with a particular persistent store, you will need to copy them; you can't just try to add them to your other persistent store coordinator.
Alex