Reputation: 791
first let me state that this is NOT a core data migration question. I'm not actually looking at changing the Core Data model, but add additional items during an app update.
The process is as follows:
1) Application is created with a pre-loaded core data. Specifically, a checklist. 2) User can edit this checklist by changing certain values (owned, wanted, etc) but CANNOT remove items 3) I release updates with new items that are added. The users existing data is NOT modified (unless I need to make changes for errata purposes)
I currently manage this by creating NSUserDefaults to check to see if a user has received an update, and if they haven't, add the new items. For example, if a user is going from version 1.4.3 to 1.4.4, the app will check and add the items added in 1.4.4. However, if the app is going from 1.4.0 to 1.4.4, it will check and add items added in 1.4.1, 1.4.2, 1.4.3, AND 1.4.4 . In addition, if a user is installing the app new at version 1.4.4, the newer items are already part of the pre-load and it knows not to apply any of the previous updates. The updates are applied as a .plist/xml file
The system currently works and works well, but it is becoming cumbersome as I now have 38 plist files in my application and 37 if/else statements checking to see if the updates are applied.
There must be a better way. My initial thoughts are to have two databases in the app... One which I update with app updates and the second that is editable by user. Then, with each app update, the database would be compared and any new items in the database would be copied over to the editable store. I'm concerned that this would be a long process though (there are currently over 37,000 items and it's fine when I'm adding 400-500 in an update, but would it take a long time to traverse 37,000 items and copy over new ones?)
I suppose this question may be too subjective for this site, and I apologize if it is, but suggestions would be greatly appreciated!
Zack
Upvotes: 0
Views: 120
Reputation: 70946
You could do this with one pre-load data store and no update property lists or if
statements like this:
Store the app current version in user defaults so that during an update you can retrieve it as the previous version (it sounds like you're already doing this, but I wanted to be clear).
In the preload store, add three integer fields that correspond to the major, minor, and point release where that preload item was added. For example, if an item was added in version 1.4.3, major
= 1, minor
= 4, and point
= 3.
When adding new items to the user's data store during an update, get the previous app version from user defaults, and get the major, minor, and point update numbers from that. If the user is upgrading from version 1.4.0, use previousMajor
= 1, previousMinor
= 4, previousPoint
= 0. Make these values default to zero, so that if the user is installing the app for the first time, all three are zero.
Fetch anything that needs to be added from the preload store using a predicate like:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"major > %d and minor > %d and point > %d",
previousMajor, previousMinor, previousPoint];
Everything that the fetch finds needs to be added. Add it, and you're done.
Upvotes: 3