Reputation: 423
i have a very complicated problem that i would like to share with you and maybe someone can answer it for me. before i start i have to say that i am very new in this.
So, i have a coredata iphone app (much like the recipes app) that uses a pre-populated sql database. The user can add/edit his own data but the default data cannot be deleted. the useres data are ALL saved in the same sql database.
QUESTION: what do i have to do in order to: - update some (not all) of the default data that are stored in the sql database without "touching" the user's data? (the model will stay the same - no new entities etc-) (if the user uninstall the app and then reinstall the new version everything will be ok but i dont want to do this, obviously).
can someone PLEASE help in coding level?
Upvotes: 4
Views: 3868
Reputation: 45598
To support adding new entities etc. later on, you want to use versioning and automatic light-weight migration which is described here:
Basically you create a new version of your data model using the Design->Data Model
menu item in Xcode, then make a few code changes. This will cause Core Data to automatically migrate an older model to the newer one. You are limited in what kinds of changes you can make. You can add new entities, and either add optional attribute to existing entities, or required attributes with default values set.
One thing that caught me out is that the way you load the core data NSManagedObjectModel
changes when you want to use versioning and migration. Without migration you probably have this:
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
Once you start using versioning and migration this needs to change to something like this:
NSString *path = [[NSBundle bundleForClass:self.class] pathForResource:@"DataModelName"
ofType:@"momd"];
NSURL *url = [NSURL fileURLWithPath:path];
NSManagedObjectModel *model = [[[NSManagedObjectModel alloc] initWithContentsOfURL:url] autorelease];
Upvotes: 2
Reputation: 2802
Core data explicitly supports model versioning and provides facilities to migrate your data between versions. This should contain the information you need. Link to Developer Docs for Migration
Upvotes: 1
Reputation: 5600
Since you have entered the default data, you must be knowing the Ids/keys for those records. All you need is an update script that would change the default data.
Upvotes: 0