Transfer / update the underlying data application without deleting user data!

I have a very difficult problem that I would like to share with you, and maybe someone can answer it for me. before I begin, I must say that I am very new to this.

So I have an iphone coredata application (similar to a recipes application) that uses a pre-populated sql database. The user can add / edit their own data, but the default data cannot be deleted. ALL user data is stored in the same SQL database.

Question: what should I do to: - update some (not all) of the default data that is stored in the SQL database without "touching" user data? (the model will remain unchanged - no new objects, etc.) (if the user uninstalls the application and then reinstalls the new version, everything will be fine, but I do not want to do this, obviously).

can anyone PLEASE help in the coding level?

+4
source share
3 answers

To subsequently add new objects, etc., you want to use version control and the automatic easy migration described here:

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmLightweight.html#//apple_ref/doc/uid/TP40008426-SW1

Basically, you create a new version of your data model using the Design->Data Model menu Design->Data Model in Xcode, and then make a few code changes. This will cause Core Data to automatically migrate the older model to the newer one. You are limited by what changes you can make. You can add new entities and add an additional attribute to existing objects or the required attributes with the default values ​​set.

One thing that caught me is that the way you load the NSManagedObjectModel master data changes when you want to use version control and migration. Without migration, you probably have the following:

 NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil]; 

Once you start using version control and migration, you need to change 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]; 
+2
source

The master data explicitly supports version versioning and provides a means to transfer your data between versions. This should contain the necessary information. Link to developer docs for migration

+1
source

Since you entered the default data, you must know the identifiers / keys for these entries. All you need is a script update that will change the default data.

0
source

Source: https://habr.com/ru/post/1308551/


All Articles