Core将属性的数据从String转换为Integer 16 [英] Core Data migration of attribute from String to Integer 16

查看:199
本文介绍了Core将属性的数据从String转换为Integer 16的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将商店实体属性从String迁移到Integer 16.以下是我采取的步骤:

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


  1. 添加模型版本...

  2. 在新模型中,将Entity属性从String更改为Int 16。

  3. 在文件检查器>数据模型>当前模型

  4. 为旧模型和新模型创建映射模型。

  5. 运行

  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

这是错误:


未解决的错误Error Domain = NSCocoaErrorDomain Code = 134140
操作无法完成(可可错误134140.)
UserInfo = 0xbd5cd20 {reason =找不到或自动推断映射
模型迁移,destinationModel = ...

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

编译的.app中有映射模型:

The mapping model is there in the compiled .app:

并在专案中:

迁移适用于像Integer 16> Integer 32或更改属性名称的属性。

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

我尝试创建一个简单的Core Data Project,有和没有映射模型)从字符串到整数16和返回。

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.

推荐答案

这是因为Core Data无法自动迁移您的属性。这是因为它不能保证一个字符串将永远适合在一个int(即使你知道你的数据)。

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. 在Xcode中,创建一个新的映射模型(文件>新建>新文件),选择映射模型核心数据部分

  2. 在向导中选择源模型和目标模型

  3. 这基本上使您处于与轻量级迁移相同的位置,自动,除非你有重写一些映射的选项。

  4. 创建一个新的映射策略类(扩展 NSEntityMigrationPolicy

  5. 实施 createDestinationInstancesForSourceInstance :entityMapping:manager:error:,它将为您提供源实例,以便您可以将该字符串转换为int并将其存储在新存储中。

  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. 然后您要做的就是设置策略在映射模型中。选择映射模型文件,选择适当的Entity映射并在右侧面板上设置CustomPolicy。

请务必更改迁移设置NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption)来删除自动类型推断(无论你在何处启动Core数据)

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];

这应该是...

这篇关于Core将属性的数据从String转换为Integer 16的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆