Basic data migration: methods in NSEntityMigrationPolicy are not called.

I have two versions of the model - 12 and 13. Then I created an xcmappingmodel-File with source 12 and destination 13.

I subclassed NSEntityMigrationPolicy and added my class to the mapmodel file of the file to the desired object.

@interface EndDateMigrationPolicy : NSEntityMigrationPolicy 

enter image description here

After installing and installing the old version (11) on my device, I set the current status with version 13 of the model - the application starts, but my migration methods are not called. Did I miss something?

EDIT: is it correct to use these parameters?

  NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES}; 
+6
source share
2 answers

I will try to answer you as best as possible, I have experienced the migration of basic data several times, so I know how painful it is. For one, you cannot hope that your migration will work with these parameters, because what you are trying to do is not really easy portability, and yet you say that it makes migration easier.

In principle, suppose that for some reason you need to have an easy migration between the 2 versions, in your case 11 and 12. What you need to do is do this:

1-> 12 light weight 12-> 13 user migrations 13 β†’ (future version) light migration

There may be a better solution, but I have not found it yet.

Here is some code that will help you (the most difficult parts, I can’t remember everything idle), before this code I copy the database to a backup, so basically the backup database is your old and your storage is new (which is empty)

 NSString *path = [[NSBundle mainBundle] pathForResource:<YOUR MODEL NAME> ofType:@"cdm"]; NSURL *backUpURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"<YOUR MODEL NAME>MigrationBackUp.sqlite"]; //or whatever you want to call your backup NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"<YOUR MODEL NAME>.sqlite"]; NSError *err2 = nil; NSDictionary *sourceMetadata2 = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:backUpURL error:&err2]; NSManagedObjectModel *sourceModel2 = [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:[NSBundle mainBundle]] forStoreMetadata:sourceMetadata2]; NSManagedObjectModel *destinationModel2 = [self managedObjectModelForVersion:@"1.4"]; //Yeah your gonna have to get your mapping model , I'll give you this code too later NSString *oldModel = [[sourceModel2 versionIdentifiers] anyObject]; NSLog(@"Source Model : %@",oldModel); NSMappingModel *mappingModel = [[NSMappingModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]]; if (mappingModel != nil) { for (NSString * identifier in [mappingModel entityMappings]) { NSLog(@"Mapping > %@",identifier); } } 

Then just do the migration with your source and destination.

This is also the hard part:

  BOOL success = [migrator migrateStoreFromURL:backUpURL type:NSSQLiteStoreType options:nil withMappingModel:mappingModel toDestinationURL:storeURL destinationType:NSSQLiteStoreType destinationOptions:nil error:&err2]; 

And last but not least (I said I will give it to you earlier):

 - (NSManagedObjectModel *)managedObjectModelForVersion:(NSString*)version { NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"Model" ofType:@"momd"]; if (BETWEEN_INEX(version, @"1.0", @"1.4")) { modelPath = [modelPath stringByAppendingPathComponent:@"Model"]; modelPath = [modelPath stringByAppendingPathExtension:@"mom"]; } else if (BETWEEN_INEX(version, @"1.4", @"1.5")) { modelPath = [modelPath stringByAppendingPathComponent:@"Model 2"]; modelPath = [modelPath stringByAppendingPathExtension:@"mom"]; } else if (BETWEEN_INEX(version, @"1.5", @"1.6")) { modelPath = [modelPath stringByAppendingPathComponent:@"Model 3"]; modelPath = [modelPath stringByAppendingPathExtension:@"mom"]; } else if (BETWEEN_INEX(version, @"1.6", @"1.7")) { modelPath = [modelPath stringByAppendingPathComponent:@"Model 4"]; modelPath = [modelPath stringByAppendingPathExtension:@"mom"]; } NSURL *modelURL = [NSURL fileURLWithPath:modelPath]; NSManagedObjectModel * oldManagedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; NSSet *vIdentifiers = [oldManagedObjectModel versionIdentifiers]; for (NSString * identifier in vIdentifiers) { NSLog(@"Old Model : %@",identifier); } return [oldManagedObjectModel autorelease]; 

}

I know these are just snippets of code, but I really hope this helps you solve your problem, if you need anything else, just ask.

+3
source

Your NSEntityMigrationPolicy is not called because you set NSInferMappingModelAutomaticallyOption to @ (YES) - it should be @ (NO)

+1
source

All Articles