0

I have a version 1.0 , currently distributed in app store. The version uses a model of version 1.0. Some of the entities in the store of the model are populated with predefined data so the app is packaged with a store already contained this data, the app doesn't let the users change these entities (only read). The store is of type SQLite.

I have found out that in version 1.0 ,there's a property in an entity (one of those predefined entities) that contains wrong value (for all instances of this entity). I would like to fix that in version 2.0.

So I thought , for version 2.0, lets change the model content so now the properties in all instances of this entity will contain the correct values. So I modified the store (didn't change the model version of scheme ) so it contain the right values.

Here is the problem , If i install version 2.0 on a device that never had version 1.0 , I can see the correct values inside the app and everything is ok. BUT , if I install version 2.0 on top of version 1.0 (update) , The app still show me the values it shown in version 1.0.

My immediate conclusion was that the new version doesn't migrate data in new version to data in the older version , Infect , I read in apple documentation that the migration is typically done from the old store to the new store. and since the old store contains wrong values , I see it also in the new store.

I came to a conclusion that I have to define a migration such that in version 2.0 , the correct values are stored.

Here is what I tried:

  1. Lightweight migration (staying with the same version model) -> still see values of version 1.0.

  2. Created a new version for he model that has the same scheme , created a mapping model that maps from model 1 to model 2.

2.a. migration using mapping model that specify that the property at the destination is filled with the property of the source -> still see the values of version 1.0.

2.b. migration using mapping model that has no rule for the property -> still see the values of version 1.0

2.c. migration using a mapping model that specify that the property of at the destination is filled with the property of the destination -> still see the values of version 1.0

2.d. light weight migration -> still…

I use the following code to initiate a migration:

 NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                         nil];

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
Yossi
  • 87
  • 7

1 Answers1

2

You are mixing up migrating data and migrating the data model. Core Data migration means that you change the model, i.e. that an entities attribute is renamed or a new entity is added etc.

In your case it just seems that you want to update that data, which is much simpler. You can do it in one of these ways:

Edit: after your clarification, this is not relevant any more: For example, you could just make a new store and copy it over the old one on the file system level. I think that this is crude and error prone.

My preferred solution would be to check the version (e.g. via NSUserDefaults or by doing a fetch), and then simply to fetch all relevant instances, update them and save.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • I forgot to note something important , the old store already contain user information which I like to preserve for the new version. If i simply override the store file , I will miss that goal. You second suggestion may work but still , isn't it possible to get the same affect using migration? – Yossi Oct 20 '13 at 15:20
  • 1
    Sorry, no - please read my answer -- *data* is not *model*. You are still misunderstanding migration. What I outline **is** migration. What the SDK offers is **model migration**. Got it? – Mundi Oct 20 '13 at 20:38
  • I agree with @Mundi - Core Data migrates the model not the data it contains..Your best bet is to write a piece of code that does the data migration for you.. this can be run on the first launch of the app to correct your data. – Sharath Oct 21 '13 at 16:56
  • That is what I suggest in my last paragraph. – Mundi Oct 21 '13 at 18:17