我正在尝试使用xcode(.xcdatamodel)创建的数据模型和在运行时以编程方式创建的映射模型手动调用CoreData迁移。 迁移似乎完成但没有错误,但新创建的数据库中没有数据。 DB的结构在那里(表,Z_METADATA等),但数据没有结转,显然我的NSMappingModel没有被应用。
总结迁移:国家/地区实体已重命名为Country2,Country.name已重命名为Country2.new_name。
default:
System.out.println ("Hello");
System.out.println ("Hello, Again");
更多信息
将我手工制作的NSMappingModel与在XCode的GUI中创建的NSMappingModel进行比较,您可以看到一些差异:
来自Xcode的GUI
NSMutableArray *attrMaps = [[NSMutableArray alloc] init];
NSPropertyMapping *pMap = [[NSPropertyMapping alloc] init];
pMap.name = @"new_name";
pMap.valueExpression = [NSExpression expressionForKeyPath:@"source.name"];
[attrMaps addObject:pMap];
NSEntityMapping *eMap = [[NSEntityMapping alloc] init];
eMap.sourceEntityName = @"Country";
eMap.destinationEntityName = @"Country2";
eMap.name = @"CountryToCountry2";
eMap.mappingType = NSTransformEntityMappingType;
eMap.attributeMappings = attrMaps;
NSMutableArray *eMaps = [[NSMutableArray alloc] init];
[eMaps addObject:eMap];
NSMappingModel *mappingModel = [[NSMappingModel alloc] init];
mappingModel.entityMappings = eMaps;
NSURL * storeURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/Documents/%@",NSHomeDirectory(),@"TestCoreData.sqlite"]];
NSURL * storeURL2 = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/Documents/%@",NSHomeDirectory(),@"TestCoreData2.sqlite"]];
NSMigrationManager *migman = [self getMigrationManager];
NSError * err;
BOOL ok = [migman migrateStoreFromURL:storeURL
type:NSSQLiteStoreType
options:nil
withMappingModel:mappingModel
toDestinationURL:storeURL2
destinationType:NSSQLiteStoreType
destinationOptions:nil
error:&err];
矿
entityMappings (
"(NSEntityMapping),
name CountryToCountry2,
mappingType 5,
sourceEntityName Country,
sourceEntityVersionHash <828c33cf 3ffdc5df dd1e73e8 4f7b60a5 f10dcb43 af26adeb 33d2c518 0c992dd8>,
destinationEntityName Country2,
destinationEntityVersionHash <8cdb8ecf cb47b39d 4b45858f a247487a dd88088b f509a6ec cc938740 5cf7a041>, attributeMappings (
"(NSPropertyMapping), name new_name, valueExpression $source.name, userInfo (null)"
), relationshipMappings (
), sourceExpression FETCH(FUNCTION($manager, "fetchRequestForSourceEntityNamed:predicateString:" , "Country", "TRUEPREDICATE"), $manager.sourceContext, NO),
entityMigrationPolicyClassName (null), userInfo (null)"
更新
我已将此sourceExpression添加到我的Entity映射中,现在Country的行被复制到新数据库,但它们都是空的。所以NSPropertyMappings似乎仍未生效:
entityMappings (
"(NSEntityMapping),
name CountryToCountry2,
mappingType 5,
sourceEntityName Country,
sourceEntityVersionHash (null),
destinationEntityName Country2,
destinationEntityVersionHash (null),
attributeMappings (
"(NSPropertyMapping), name new_name, valueExpression source.name, userInfo (null)",
), relationshipMappings (
), sourceExpression (null),
entityMigrationPolicyClassName (null), userInfo (null)"
答案 0 :(得分:1)
NSPropertyMapping的valueExpression定义不正确。它应该是expressionWithFormat:
而不是expressionForKeyPath:
而$source
不是source
。像这样:
pMap.valueExpression = [NSExpression expressionWithFormat:@"$source.name"];
有了这个,我有一个手工制作的NSMappingModel实际工作。