Reputation: 3541
I have a Core Data model with five with various kinds of relationships. Most of the time, these data are created by the user on the device (the entities include such things as jpegs and descriptive text) and are in a sqlite persistent store.
However, I also want to offer in-app purchases, each of which will consist of data for three of the five entities. The other two entities relate solely to the core data objects created by the user on the device and thus will not be part of the in-app purchase. The amount of data in any one download is small (100-300K).
After purchased content is downloaded, I need to be able to integrate it into my core data store. So, I'm trying to understand the best (or at least a "good") way of doing this.
My current thinking is to distribute the three entities as SQLite tables and import those into Core Data. I wonder, though, if someone can suggest a better alternative I'm overlooking. I'm not looking for code here (well, that would be great, but...). I'll also need to export user-entered data on one device to move to another [which I hope to do with Airdrop, but that's a different story].
Any suggestions will be greatly appreciated, even if a bit off-the-wall.
Upvotes: 0
Views: 51
Reputation: 21244
Using Core Data generated SQLite files is a reasonable solution. You should be aware that the SQLite pragmas used to generate and read the data should be consistent, and be aware that different options can create more than one file (all of these files are necessary). See Technical Q&A 1809 for some more detail on the default pragmas, as well as the WWDC 2013 Session "What's new in Core Data and iCloud". Of particular note, the default journaling mode of WAL is not recommended for files that will be moved around (journal mode DELETE, the old way, may be preferable).
In a nutshell, the application would download the Core Data generated SQLite files, and you would perform a migration to import these into the store. Note that the out-of-the box migration managers do not prevent duplicates, you would need to implement your own migration process to do so (it's likely that you will want to, since it's possible that the same content could be imported more than once).
An example of this process would be...
[coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:configuration URL:downloadedStuffURL options:options error:&error]
Note that I would also suggest including NSReadOnlyPersistentStoreOption
as part of the options, so nothing attempts to write to these files.
Perform a migration, using your primary application data store URL as the destination:
store = [coordinator persistentStoreForURL:downloadedStuffURL]; sqlStore = [coordinator migratePersistentStore:store toURL:applicationStoreURL options:options withType:NSSQLiteStoreType error:&error];
And there you go. The data from the store located at downloadedStuffURL will now be imported into the store located at applicationStoreURL. The downloadedStuffURL store will be removed from the coordinator and the applicationStoreURL store will be added, with the imported data.
Upvotes: 1