Core Data migration of attribute from String to Integer 16

前端 未结 2 1231
执念已碎
执念已碎 2020-12-23 15:06

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, ch
相关标签:
2条回答
  • 2020-12-23 15:38

    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...

    0 讨论(0)
  • 2020-12-23 15:42

    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 =\

    0 讨论(0)
提交回复
热议问题