17

I'm having trouble migrating a store entity attribute from String to Integer 16. Here are the steps I take:

  1. Add Model Version...
  2. In the new model, change Entity attribute from String to Int 16.
  3. Select the new model in File Inspector > Versioned Core Data Model > Current Model
  4. Create a mapping model for the old and new models.
  5. Run

Here is the error:

Unresolved error Error Domain=NSCocoaErrorDomain Code=134140 "The operation couldn’t be completed. (Cocoa error 134140.)" UserInfo=0xbd5cd20 {reason=Can't find or automatically infer mapping model for migration, destinationModel=...

The mapping model is there in the compiled .app:

Bundle

and in the Project:

enter image description here

Migration works for attributes like Integer 16 > Integer 32, or when changing attribute names.

I tried creating a simple Core Data Project and migration worked automatically (with and without mapping model) from String to Integer 16 and back.

The strangest part is I tried looking programatically for all mapping models in the bundle and none are found for the current source/destination models.

Victor Bogdan
  • 2,022
  • 24
  • 33

2 Answers2

23

This happens because Core Data is unable to automatically migrate your attribute. This is because it can't guarantee that a string will always fit in an int (even though you know your data does).

So what you need to do is use a mapping model. Here's how to do it:

  1. In Xcode, create a new mapping model (File > New > New File), select Mapping Model in the Core Data section
  2. Select the source and target models in the wizard
  3. This basically puts you in the same place as the lightweight migration, everything is done automatically, except you have the option to override some mapping. Specifically, that one that is giving you troubles.
  4. Create a new mapping policy class (Extend NSEntityMigrationPolicy)
  5. Implement createDestinationInstancesForSourceInstance:entityMapping:manager:error: which will give you the source instance so you can convert that string into an int and store it in the new store.

Your code should look something like this:

- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
    NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:[mapping destinationEntityName] inManagedObjectContext:[manager destinationContext]];  

    // Copy all the values from sInstance into newObject, making sure to apply the conversion for the string to int when appropriate. So you should have one of these for each attribute:
    [newObject setValue:[sInstance valueForKey:@"xyz"] forKey:@"xyz"];

    [manager associateSourceInstance:sInstance withDestinationInstance:newObject forEntityMapping:mapping];
}
  1. Then all you have to do is set that policy in the mapping model. Select the mapping model file, pick the appropriate Entity mapping and set the CustomPolicy on the right panel.

Be sure to change the migration settings to remove automatic type inference wherever you init Core Data

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

That should be it...

emig
  • 25
  • 3
mprivat
  • 21,582
  • 4
  • 54
  • 64
  • Thanks for your reply, but I already have a policy subclass and a breakpoint inside it, eagerly waiting to be hit - the problem is that it just can't find the model mapping file... – Victor Bogdan May 31 '12 at 13:54
  • This error "Can't find or automatically infer mapping model for migration" is saying that Core Data is still trying to infer types and not using a mapping model. Can you double check that you've set the options right when initing Core Data? (last line of my reply) – mprivat May 31 '12 at 14:01
  • Yep, and I also added NSInferMappingModelAutomaticallyOption. Without it, the error is "Can't find mapping model". – Victor Bogdan May 31 '12 at 14:07
  • You don't want `NSInferMappingModelAutomaticallyOption` or else it won't try to use the mapping model. The problem to solve is making sure it finds the mapping file. – mprivat May 31 '12 at 14:20
  • That makes sense.. I tried it and the error is: Unresolved error Error Domain=NSCocoaErrorDomain Code=134140 "The operation couldn’t be completed. (Cocoa error 134140.)" UserInfo=0x6127550 {destinationModel=() isEditable 0, entities { }, fetch request templates { }, sourceModel=() isEditable 1, entities {}, fetch request templates {}, reason=Can't find mapping model for migration}, destinationModel = "() isEditable 0, entities {}, reason = "Can't find mapping model for migration"; – Victor Bogdan May 31 '12 at 14:31
  • I wonder if you are running into this [post](http://stackoverflow.com/questions/5369872/core-data-migration-cant-find-mapping-model-for-migration) – mprivat May 31 '12 at 14:59
  • Nothing in that post seemed to help... I don't know what else to try. – Victor Bogdan Jun 04 '12 at 08:27
  • After enabling Core Data Migration debug (only works on iOS Simulator 5.0+), I reached the actual [problem](http://stackoverflow.com/questions/10894383/core-data-mapping-model-version-hashes-not-matching-to-source-model-version-hash). – Victor Bogdan Jun 05 '12 at 09:03
  • 1
    I finally solved problems with entity hashes by adding a new model version and removing those entities, then adding another version and adding the entities back. From that point on, following the usual steps of creating a new model and changing attribute types from String to Int16 finally worked as expected. – Victor Bogdan Jun 26 '12 at 09:33
1

For those, who broked thousands of spears on "Can't find mapping model for migration" error, this might help:

  1. Make sure, you created mapping file in proper folder/group (before pressing Cmd+N - select .xcdatamodeld file in project navigator).
  2. Clean the project.
  3. Rebuild the project and run.

In my case, app automagically found the mapping model after clean/rebuild =\

ankhzet
  • 2,517
  • 1
  • 24
  • 31